-\r
- \r
- /**\r
- * Create a concatenation of two references. Prefix part is cloned,\r
- * suffix is linked.\r
- * \r
- * @param pathToBeCloned prefix path, or <tt>null</tt>\r
- * @param ref suffix path, or <tt>null</tt>\r
- * @return path\r
- */\r
- public static ChildReference concatenate(ChildReference pathToBeCloned, ChildReference ref) {\r
- if (pathToBeCloned==null) return ref;\r
- ChildReference result = pathToBeCloned.clone();\r
- if (ref==null) return result;\r
- result.tail().setChildReference(ref);\r
- return result;\r
- }\r
- \r
- /**\r
- * Creates a compilation of individual a references into a one refence. \r
- * \r
- * @param refs\r
- * @return reference or <code>null</code> if there are no elements\r
- */\r
- public static ChildReference compile(ChildReference ... refs) {\r
- if (refs.length==0) return null;\r
- ChildReference first = refs[0].clone();\r
- ChildReference r = first;\r
- for (int i=1; i<refs.length; i++) {\r
- ChildReference next = refs[i].clone();\r
- r.setChildReference( next );\r
- r = next;\r
- while(r.childReference!=null) r = r.childReference;\r
- }\r
- return first;\r
- }\r
-\r
- /**\r
- * Creates a compilation of individual a references into a one refence. \r
- * \r
- * @param refs\r
- * @return reference or <code>null</code> if there are no elements\r
- */\r
- public static ChildReference compile(Collection<ChildReference> refs) {\r
- if (refs.isEmpty()) return null;\r
- Iterator<ChildReference> itr = refs.iterator();\r
- ChildReference first = itr.next().clone();\r
- ChildReference r = first;\r
- for (; itr.hasNext(); ) {\r
- ChildReference next = itr.next().clone();\r
- r.setChildReference( next );\r
- r = next;\r
- while(r.childReference!=null) r = r.childReference;\r
- }\r
- return first;\r
- }\r
-\r
- /**\r
- * Get reference path from AccessorReference path.\r
- * <a href="http://dev.simantics.org/index.php/Databoard_Specification#Path_Notation">Path Notation</a>\r
- * \r
- * @param path\r
- * @return reference path or <code>null</code> if there is no path\r
- */\r
- public static ChildReference parsePath(String path) {\r
- StringTokenizer st = new StringTokenizer(path, "/", false);\r
- if (!st.hasMoreTokens()) return null;\r
- ChildReference first = createSingleReference( st.nextToken() ); \r
- ChildReference ref = first;\r
- while (st.hasMoreTokens()) {\r
- ref.childReference = createSingleReference(st.nextToken());\r
- ref = ref.childReference;\r
- }\r
- \r
- return first;\r
- }\r
- \r
- /**\r
- * Attempt to convert value reference to type reference. \r
- * \r
- * @param vref\r
- * @param type\r
- * @return type reference or null\r
- * @throws IllegalArgumentException if conversion fails.\r
- */\r
- public static ChildReference toTypeReference(ChildReference vref, Datatype type) \r
- throws IllegalArgumentException \r
- {\r
- if (vref==null) return null;\r
- if (type instanceof ArrayType) {\r
- if (vref instanceof IndexReference || vref instanceof LabelReference) {\r
- ChildReference tail = toTypeReference( vref.childReference, type.getComponentType(0) );\r
- return new ComponentReference( tail );\r
- }\r
- }\r
- \r
- if (type instanceof MapType) {\r
- if (vref instanceof KeyReference) {\r
- ChildReference tail = toTypeReference( vref.childReference, type.getComponentType(1) );\r
- return new IndexReference( 1, tail ); \r
- }\r
- }\r
- \r
- if (type instanceof OptionalType) {\r
- if (vref instanceof ComponentReference) {\r
- ChildReference tail = toTypeReference( vref.childReference, type.getComponentType(0) );\r
- return new ComponentReference( tail ); \r
- }\r
- if (vref instanceof LabelReference) {\r
- LabelReference lr = (LabelReference) vref;\r
- if (lr.label.equals("v")) {\r
- ChildReference tail = toTypeReference( vref.childReference, type.getComponentType(0) );\r
- return new ComponentReference( tail );\r
- }\r
- } \r
- }\r
- \r
- if (type instanceof RecordType) {\r
- RecordType rt = (RecordType) type;\r
- if (vref instanceof IndexReference) {\r
- IndexReference ir = (IndexReference) vref; \r
- ChildReference tail = toTypeReference( vref.childReference, type.getComponentType(ir.index) );\r
- return new IndexReference( ir.index, tail );\r
- }\r
- \r
- if (vref instanceof NameReference) {\r
- NameReference ir = (NameReference) vref; \r
- ChildReference tail = toTypeReference( vref.childReference, rt.getComponentType(ir.name) );\r
- return new NameReference( ir.name, tail );\r
- }\r
- \r
- if (vref instanceof LabelReference) {\r
- LabelReference ir = (LabelReference) vref; \r
- ChildReference tail = toTypeReference( vref.childReference, rt.getComponentType(ir.label) );\r
- return new NameReference( ir.label, tail );\r
- } \r
- }\r
- \r
- if (type instanceof UnionType) {\r
- UnionType ut = (UnionType) type;\r
- if (vref instanceof IndexReference) {\r
- IndexReference ir = (IndexReference) vref; \r
- ChildReference tail = toTypeReference( vref.childReference, type.getComponentType(ir.index) );\r
- return new IndexReference( ir.index, tail );\r
- }\r
- \r
- if (vref instanceof NameReference) {\r
- NameReference ir = (NameReference) vref; \r
- ChildReference tail = toTypeReference( vref.childReference, ut.getComponentType(ir.name) );\r
- return new NameReference( ir.name, tail );\r
- }\r
- \r
- if (vref instanceof LabelReference) {\r
- LabelReference ir = (LabelReference) vref; \r
- ChildReference tail = toTypeReference( vref.childReference, ut.getComponentType(ir.label) );\r
- return new NameReference( ir.label, tail );\r
- } \r
- }\r
- \r
- \r
- throw new IllegalArgumentException();\r
- }\r
- \r
- public static ChildReference parseBinary(byte[] binaryRef) \r
- throws IOException \r
- {\r
- Binding binding = Bindings.getBindingUnchecked(ChildReference.class);\r
- ChildReference result;\r
- result = (ChildReference) Bindings.getSerializerUnchecked( binding ).deserialize(binaryRef);\r
- return result;\r
- }\r
- \r
- public final static Pattern INDEX_PATTERN = Pattern.compile("i-(\\d*)");\r
- public final static Pattern MAP_PATTERN = Pattern.compile("k-(\\p{ASCII}*)");\r
- public final static Pattern NAME_PATTERN = Pattern.compile("n-(\\p{ASCII}*)");\r
+
+
+ /**
+ * Create a concatenation of two references. Prefix part is cloned,
+ * suffix is linked.
+ *
+ * @param pathToBeCloned prefix path, or <tt>null</tt>
+ * @param ref suffix path, or <tt>null</tt>
+ * @return path
+ */
+ public static ChildReference concatenate(ChildReference pathToBeCloned, ChildReference ref) {
+ if (pathToBeCloned==null) return ref;
+ ChildReference result = pathToBeCloned.clone();
+ if (ref==null) return result;
+ result.tail().setChildReference(ref);
+ return result;
+ }
+
+ /**
+ * Creates a compilation of individual a references into a one refence.
+ *
+ * @param refs
+ * @return reference or <code>null</code> if there are no elements
+ */
+ public static ChildReference compile(ChildReference ... refs) {
+ if (refs.length==0) return null;
+ ChildReference first = refs[0].clone();
+ ChildReference r = first;
+ for (int i=1; i<refs.length; i++) {
+ ChildReference next = refs[i].clone();
+ r.setChildReference( next );
+ r = next;
+ while(r.childReference!=null) r = r.childReference;
+ }
+ return first;
+ }
+
+ /**
+ * Creates a compilation of individual a references into a one refence.
+ *
+ * @param refs
+ * @return reference or <code>null</code> if there are no elements
+ */
+ public static ChildReference compile(Collection<ChildReference> refs) {
+ if (refs.isEmpty()) return null;
+ Iterator<ChildReference> itr = refs.iterator();
+ ChildReference first = itr.next().clone();
+ ChildReference r = first;
+ for (; itr.hasNext(); ) {
+ ChildReference next = itr.next().clone();
+ r.setChildReference( next );
+ r = next;
+ while(r.childReference!=null) r = r.childReference;
+ }
+ return first;
+ }
+
+ /**
+ * Get reference path from AccessorReference path.
+ * <a href="http://dev.simantics.org/index.php/Databoard_Specification#Path_Notation">Path Notation</a>
+ *
+ * @param path
+ * @return reference path or <code>null</code> if there is no path
+ */
+ public static ChildReference parsePath(String path) {
+ StringTokenizer st = new StringTokenizer(path, "/", false);
+ if (!st.hasMoreTokens()) return null;
+ ChildReference first = createSingleReference( st.nextToken() );
+ ChildReference ref = first;
+ while (st.hasMoreTokens()) {
+ ref.childReference = createSingleReference(st.nextToken());
+ ref = ref.childReference;
+ }
+
+ return first;
+ }
+
+ /**
+ * Attempt to convert value reference to type reference.
+ *
+ * @param vref
+ * @param type
+ * @return type reference or null
+ * @throws IllegalArgumentException if conversion fails.
+ */
+ public static ChildReference toTypeReference(ChildReference vref, Datatype type)
+ throws IllegalArgumentException
+ {
+ if (vref==null) return null;
+ if (type instanceof ArrayType) {
+ if (vref instanceof IndexReference || vref instanceof LabelReference) {
+ ChildReference tail = toTypeReference( vref.childReference, type.getComponentType(0) );
+ return new ComponentReference( tail );
+ }
+ }
+
+ if (type instanceof MapType) {
+ if (vref instanceof KeyReference) {
+ ChildReference tail = toTypeReference( vref.childReference, type.getComponentType(1) );
+ return new IndexReference( 1, tail );
+ }
+ }
+
+ if (type instanceof OptionalType) {
+ if (vref instanceof ComponentReference) {
+ ChildReference tail = toTypeReference( vref.childReference, type.getComponentType(0) );
+ return new ComponentReference( tail );
+ }
+ if (vref instanceof LabelReference) {
+ LabelReference lr = (LabelReference) vref;
+ if (lr.label.equals("v")) {
+ ChildReference tail = toTypeReference( vref.childReference, type.getComponentType(0) );
+ return new ComponentReference( tail );
+ }
+ }
+ }
+
+ if (type instanceof RecordType) {
+ RecordType rt = (RecordType) type;
+ if (vref instanceof IndexReference) {
+ IndexReference ir = (IndexReference) vref;
+ ChildReference tail = toTypeReference( vref.childReference, type.getComponentType(ir.index) );
+ return new IndexReference( ir.index, tail );
+ }
+
+ if (vref instanceof NameReference) {
+ NameReference ir = (NameReference) vref;
+ ChildReference tail = toTypeReference( vref.childReference, rt.getComponentType(ir.name) );
+ return new NameReference( ir.name, tail );
+ }
+
+ if (vref instanceof LabelReference) {
+ LabelReference ir = (LabelReference) vref;
+ ChildReference tail = toTypeReference( vref.childReference, rt.getComponentType(ir.label) );
+ return new NameReference( ir.label, tail );
+ }
+ }
+
+ if (type instanceof UnionType) {
+ UnionType ut = (UnionType) type;
+ if (vref instanceof IndexReference) {
+ IndexReference ir = (IndexReference) vref;
+ ChildReference tail = toTypeReference( vref.childReference, type.getComponentType(ir.index) );
+ return new IndexReference( ir.index, tail );
+ }
+
+ if (vref instanceof NameReference) {
+ NameReference ir = (NameReference) vref;
+ ChildReference tail = toTypeReference( vref.childReference, ut.getComponentType(ir.name) );
+ return new NameReference( ir.name, tail );
+ }
+
+ if (vref instanceof LabelReference) {
+ LabelReference ir = (LabelReference) vref;
+ ChildReference tail = toTypeReference( vref.childReference, ut.getComponentType(ir.label) );
+ return new NameReference( ir.label, tail );
+ }
+ }
+
+
+ throw new IllegalArgumentException();
+ }
+
+ public static ChildReference parseBinary(byte[] binaryRef)
+ throws IOException
+ {
+ Binding binding = Bindings.getBindingUnchecked(ChildReference.class);
+ ChildReference result;
+ result = (ChildReference) Bindings.getSerializerUnchecked( binding ).deserialize(binaryRef);
+ return result;
+ }
+
+ public final static Pattern INDEX_PATTERN = Pattern.compile("i-(\\d*)");
+ public final static Pattern MAP_PATTERN = Pattern.compile("k-(\\p{ASCII}*)");
+ public final static Pattern NAME_PATTERN = Pattern.compile("n-(\\p{ASCII}*)");