@Override public Converter createConverter(Class<?> source, Class<?> target, Hints hints) { final FieldType sourceBinding = FieldType.forBinding(source); final FieldType targetBinding = FieldType.forBinding(target); if (UNKNOWN == sourceBinding || UNKNOWN == targetBinding) { return null; } final boolean sourceIsArray = sourceBinding.getBinding().isArray(); final boolean targetIsArray = targetBinding.getBinding().isArray(); if (!sourceIsArray && !targetIsArray) { return null; } if (String.class.equals(sourceBinding.getBinding())) { return FROM_STRING; } if (String.class.equals(targetBinding.getBinding())) { return TO_STRING; } return null; }
@Override public void writeMap(Map<String, Object> map, DataOutput data) throws IOException { data.writeInt(map.size()); for (Entry<String, Object> e : map.entrySet()) { String key = e.getKey(); writeString(key, data); Object value = e.getValue(); FieldType fieldType = FieldType.forValue(value); data.writeByte(fieldType.getTag()); encode(fieldType, value, data); } }
private Object safeCopy(@Nullable Object value) { FieldType fieldType = FieldType.forValue(value); if (FieldType.UNKNOWN.equals(fieldType)) { throw new IllegalArgumentException(String.format( "Objects of class %s are not supported as RevFeature attributes: ", value.getClass().getName())); } value = fieldType.safeCopy(value); return value; }
private void printAttributeDescriptor(Writer w, PropertyDescriptor attrib) throws IOException { print(w, attrib.getName().toString()); print(w, "\t"); print(w, FieldType.forBinding(attrib.getType().getBinding()).name()); print(w, "\t"); print(w, Integer.toString(attrib.getMinOccurs())); print(w, "\t"); print(w, Integer.toString(attrib.getMaxOccurs())); print(w, "\t"); print(w, Boolean.toString(attrib.isNillable())); PropertyType attrType = attrib.getType(); if (attrType instanceof GeometryType) { GeometryType gt = (GeometryType) attrType; CoordinateReferenceSystem crs = gt.getCoordinateReferenceSystem(); String crsText = CrsTextSerializer.serialize(crs); print(w, "\t"); println(w, crsText); } else { println(w, ""); } }
private static void writePropertyType(PropertyType type, DataOutput data) throws IOException { writeName(type.getName(), data); data.writeByte(FieldType.forBinding(type.getBinding()).getTag()); if (type instanceof GeometryType) { GeometryType gType = (GeometryType) type;
private static AttributeType readAttributeType(DataInput in, FeatureTypeFactory typeFactory) throws IOException { final Name name = readName(in); final byte typeTag = in.readByte(); final FieldType type = FieldType.valueOf(typeTag); if (Geometry.class.isAssignableFrom(type.getBinding())) { final boolean isCRSCode = in.readBoolean(); // as opposed to a raw WKT string final String crsText = in.readUTF(); final CoordinateReferenceSystem crs; try { if (isCRSCode) { if (RevObjects.NULL_CRS_IDENTIFIER.equals(crsText)) { crs = null; } else { boolean forceLongitudeFirst = crsText.startsWith("EPSG:"); crs = CRS.decode(crsText, forceLongitudeFirst); } } else { crs = CRS.parseWKT(crsText); } } catch (FactoryException e) { throw new RuntimeException(e); } return typeFactory.createGeometryType(name, type.getBinding(), crs, false, false, Collections.<Filter> emptyList(), null, null); } else { return typeFactory.createAttributeType(name, type.getBinding(), false, false, Collections.<Filter> emptyList(), null, null); } }
/** * Resolves the {@code FieldType} corresponding to the {@code value}'s class * * @see #forBinding(Class) */ public static FieldType forValue(@Nullable Object value) { if (value == null) { return NULL; } Class<?> fieldClass = value.getClass(); return forBinding(fieldClass); }
/** * @return the {@code FieldType} associated to the provided binding, or * {@link FieldType#UNKNOWN} if no {@code FieldType} relates to the argument attribute * type. */ public static FieldType forBinding(@Nullable Class<?> binding) { if (binding == null || Void.class.equals(binding)) { return NULL; } // try a hash lookup first FieldType fieldType = BINDING_MAPPING.get(binding); if (fieldType != null) { return fieldType; } // not in the map, lets check one by one anyways // beware for this to work properly FieldTypes for super classes must be defined _after_ // any subclass (i.e. Point before Geometry) for (FieldType t : values()) { if (t.getBinding() != null && t.getBinding().isAssignableFrom(binding)) { return t; } } return UNKNOWN; }
private Object parseAttribute(String line) { List<String> tokens = newArrayList(Splitter.on('\t').split(line)); Preconditions.checkArgument(tokens.size() == 2, "Wrong attribute definition: %s", line); String typeName = tokens.get(0); String value = tokens.get(1); FieldType type; try { type = FieldType.valueOf(typeName); } catch (IllegalArgumentException e) { throw new IllegalArgumentException("Wrong type name: " + typeName); } return TextValueSerializer.fromString(type, value); }
/** * @return the {@code FieldType} corresponding to the {@link Optional}'s value. * @see #forValue(Object) */ public static FieldType forValue(Optional<Object> field) { return forValue(field.orNull()); }
@Override protected void print(RevFeature feature, Writer w) throws IOException { for (int i = 0; i < feature.size(); i++) { Optional<Object> opt = feature.get(i); final FieldType type = FieldType.forValue(opt); String valueString = TextValueSerializer.asString(opt); println(w, type.toString() + "\t" + valueString); } w.flush(); }
public static void write(NodeId id, DataOutput out) throws IOException { checkNotNull(id); checkNotNull(out); final String name = id.name(); @Nullable final Object value = id.value(); final FieldType valueType = FieldType.forValue((Object) id.value()); out.writeUTF(name); out.writeByte(valueType.ordinal()); DataStreamValueSerializerV2.INSTANCE.encode(valueType, value, out); }
@Override public Optional<Geometry> get(int index, GeometryFactory gf) { final int offset = offsets[index]; final int tagValue = data[offset] & 0xFF; final FieldType type = FieldType.valueOf(tagValue); if (FieldType.NULL.equals(type)) { return Optional.absent(); } DataInput in = ByteStreams.newDataInput(data, offset + 1); Geometry value; try { value = valueParser.readGeometry(in, gf); } catch (IOException e) { throw new RuntimeException(e); } return Optional.of(value); }
@Test public void testValueOf() { assertEquals(FieldType.NULL, FieldType.valueOf(FieldType.NULL.getTag())); assertEquals(FieldType.BOOLEAN, FieldType.valueOf(FieldType.BOOLEAN.getTag())); assertEquals(FieldType.BYTE, FieldType.valueOf(FieldType.BYTE.getTag())); assertEquals(FieldType.SHORT, FieldType.valueOf(FieldType.SHORT.getTag())); assertEquals(FieldType.INTEGER, FieldType.valueOf(FieldType.INTEGER.getTag())); assertEquals(FieldType.LONG, FieldType.valueOf(FieldType.LONG.getTag())); assertEquals(FieldType.FLOAT, FieldType.valueOf(FieldType.FLOAT.getTag())); assertEquals(FieldType.DOUBLE, FieldType.valueOf(FieldType.DOUBLE.getTag())); assertEquals(FieldType.STRING, FieldType.valueOf(FieldType.STRING.getTag())); assertEquals(FieldType.BOOLEAN_ARRAY, FieldType.valueOf(FieldType.BOOLEAN_ARRAY.getTag())); assertEquals(FieldType.BYTE_ARRAY, FieldType.valueOf(FieldType.BYTE_ARRAY.getTag())); assertEquals(FieldType.SHORT_ARRAY, FieldType.valueOf(FieldType.SHORT_ARRAY.getTag())); assertEquals(FieldType.INTEGER_ARRAY, FieldType.valueOf(FieldType.INTEGER_ARRAY.getTag())); assertEquals(FieldType.LONG_ARRAY, FieldType.valueOf(FieldType.LONG_ARRAY.getTag())); assertEquals(FieldType.FLOAT_ARRAY, FieldType.valueOf(FieldType.FLOAT_ARRAY.getTag())); assertEquals(FieldType.DOUBLE_ARRAY, FieldType.valueOf(FieldType.DOUBLE_ARRAY.getTag())); assertEquals(FieldType.STRING_ARRAY, FieldType.valueOf(FieldType.STRING_ARRAY.getTag())); assertEquals(FieldType.POINT, FieldType.valueOf(FieldType.POINT.getTag())); assertEquals(FieldType.LINESTRING, FieldType.valueOf(FieldType.LINESTRING.getTag())); assertEquals(FieldType.POLYGON, FieldType.valueOf(FieldType.POLYGON.getTag())); assertEquals(FieldType.MULTIPOINT, FieldType.valueOf(FieldType.MULTIPOINT.getTag())); assertEquals(FieldType.MULTILINESTRING, FieldType.valueOf(FieldType.MULTILINESTRING.getTag())); assertEquals(FieldType.MULTIPOLYGON, FieldType.valueOf(FieldType.MULTIPOLYGON.getTag())); assertEquals(FieldType.GEOMETRYCOLLECTION, FieldType.valueOf(FieldType.GEOMETRYCOLLECTION.getTag())); assertEquals(FieldType.GEOMETRY, FieldType.valueOf(FieldType.GEOMETRY.getTag())); assertEquals(FieldType.UUID, FieldType.valueOf(FieldType.UUID.getTag()));
@Test public void testFieldTypes() { assertEquals(Void.class, FieldType.NULL.getBinding()); assertEquals(Boolean.class, FieldType.BOOLEAN.getBinding()); assertEquals(Byte.class, FieldType.BYTE.getBinding()); assertEquals(Short.class, FieldType.SHORT.getBinding()); assertEquals(Integer.class, FieldType.INTEGER.getBinding()); assertEquals(Long.class, FieldType.LONG.getBinding()); assertEquals(Float.class, FieldType.FLOAT.getBinding()); assertEquals(Double.class, FieldType.DOUBLE.getBinding()); assertEquals(String.class, FieldType.STRING.getBinding()); assertEquals(boolean[].class, FieldType.BOOLEAN_ARRAY.getBinding()); assertEquals(byte[].class, FieldType.BYTE_ARRAY.getBinding()); assertEquals(short[].class, FieldType.SHORT_ARRAY.getBinding()); assertEquals(int[].class, FieldType.INTEGER_ARRAY.getBinding()); assertEquals(long[].class, FieldType.LONG_ARRAY.getBinding()); assertEquals(float[].class, FieldType.FLOAT_ARRAY.getBinding()); assertEquals(double[].class, FieldType.DOUBLE_ARRAY.getBinding()); assertEquals(String[].class, FieldType.STRING_ARRAY.getBinding()); assertEquals(Point.class, FieldType.POINT.getBinding()); assertEquals(LineString.class, FieldType.LINESTRING.getBinding()); assertEquals(Polygon.class, FieldType.POLYGON.getBinding()); assertEquals(MultiPoint.class, FieldType.MULTIPOINT.getBinding()); assertEquals(MultiLineString.class, FieldType.MULTILINESTRING.getBinding()); assertEquals(MultiPolygon.class, FieldType.MULTIPOLYGON.getBinding()); assertEquals(GeometryCollection.class, FieldType.GEOMETRYCOLLECTION.getBinding()); assertEquals(Geometry.class, FieldType.GEOMETRY.getBinding()); assertEquals(UUID.class, FieldType.UUID.getBinding()); assertEquals(BigInteger.class, FieldType.BIG_INTEGER.getBinding()); assertEquals(BigDecimal.class, FieldType.BIG_DECIMAL.getBinding());
@Test public void TestCatFeatureObject() { RevFeature feature = RevFeatureBuilder.build(points1); CharSequence desc = geogig.command(CatObject.class).setObject(Suppliers.ofInstance(feature)) .call(); String[] lines = desc.toString().split("\n"); assertEquals(points1.getProperties().size() + 2, lines.length); assertEquals(FieldType.STRING.name() + "\tStringProp1_1", lines[2]); assertEquals(FieldType.INTEGER.name() + "\t1000", lines[3]); assertEquals(FieldType.POINT.name() + "\tPOINT (1 1)", lines[4]); }
@Test public void testSafeCopy() { byte[] original = new byte[] { (byte) 0x01, (byte) 0x02 }; byte[] copy = (byte[]) FieldType.BYTE_ARRAY.safeCopy(original); assertNotSame(original, copy); assertEquals(original[0], copy[0]); assertEquals(original[1], copy[1]); } }
public boolean equals(Object o) { if (o instanceof AttributeDefinition) { AttributeDefinition at = (AttributeDefinition) o; return name.equals(at.name) && at.type.equals(type); } else { return false; } }
private List<Object> createValuesAllSupportedTypes() { FieldType[] types = FieldType.values(); List<Object> values = new ArrayList<>(); for (FieldType ft : types) { if (ft != FieldType.UNKNOWN) { values.add(RevObjectTestUtil.sampleValue(ft)); } } return values; }