Repository navigation
Expand file tree
/
Copy pathHXStr.java
More file actions
87 lines (73 loc) · 1.97 KB
/
Copy pathHXStr.java
File metadata and controls
87 lines (73 loc) · 1.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
//
// Copyright (c) 2016, Brian Frank
// Licensed under the Academic Free License version 3.0
//
// History:
// 09 June 2016 Matthew Giannini Creation
//
package org.projecthaystack;
import org.projecthaystack.io.HZincWriter;
/**
* XStr is an extended string which is a type name and value
* encoded as a string. It is used as a generic value when an
* XStr is decoded without any predefined type.
*/
public class HXStr extends HVal
{
public static HVal decode(String type, String val)
{
if ("Bin".equals(type)) return HBin.make(val);
return new HXStr(type, val);
}
public static HXStr encode(Object val)
{
return new HXStr(val.getClass().getSimpleName(), val.toString());
}
private HXStr(String type, String val)
{
if (!isValidType(type)) throw new IllegalArgumentException("Invalid type name: " + type);
this.type = type;
this.val = val;
}
private static boolean isValidType(String t)
{
if (t == null || t.isEmpty() || !Character.isUpperCase(t.charAt(0))) return false;
char[] chars = t.toCharArray();
for (int i=0; i<chars.length; ++i)
{
if (Character.isLetter(chars[i])) continue;
if (Character.isDigit(chars[i])) continue;
if (chars[i] == '_') continue;
return false;
}
return true;
}
/** Type name */
public final String type;
/** String value */
public final String val;
public String toZinc()
{
StringBuffer s = new StringBuffer();
s.append(type).append("(\"").append(val).append("\")");
return s.toString();
}
public String toJson()
{
throw new UnsupportedOperationException();
}
public boolean equals(Object o)
{
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
HXStr hxStr = (HXStr) o;
if (!type.equals(hxStr.type)) return false;
return val.equals(hxStr.val);
}
public int hashCode()
{
int result = type.hashCode();
result = 31 * result + val.hashCode();
return result;
}
}