@Override public Converter getConverter(String value) { return Conversions.convert(value, type.getBaseType()); }
@Override public Converter getConverter(String value) { return Conversions.convert(value, type.getBaseType()); }
/** * @throws IllegalArgumentException if {@code type} is not one of the * values defined in {@link Type} or if {@code type.isArray()} is {@code true} * @throws IndexOutOfBoundsException if {@code index >= count()}. */ @SuppressWarnings("unchecked") @NotNull @Override public <S> S getValue(Type<S> type, int index) { checkArgument(!type.isArray(), "Type must not be an array type"); if (getType().getBaseType() == type) { return (S) values.get(index); } else { return convertTo(type, index); } }
/** * @throws IllegalArgumentException if {@code type} is not one of the * values defined in {@link Type} or if {@code type.isArray()} is {@code true} * @throws IndexOutOfBoundsException if {@code index >= count()}. */ @SuppressWarnings("unchecked") @Nonnull @Override public <S> S getValue(Type<S> type, int index) { checkArgument(!type.isArray(), "Type must not be an array type"); if (getType().getBaseType() == type) { return (S) values.get(index); } else { return convertTo(type, index); } }
/** * @see Object#equals(Object) */ @Override public boolean equals(Object other) { if (other instanceof ValueImpl) { ValueImpl that = (ValueImpl) other; Type<?> thisType = this.type; if (thisType.isArray()) { thisType = thisType.getBaseType(); } Type<?> thatType = that.type; if (thatType.isArray()) { thatType = thatType.getBaseType(); } try { return thisType == thatType && Objects.equal( getValue(thatType, index), that.getValue(thatType, that.index)); } catch (RepositoryException e) { LOG.warn("Error while comparing values", e); return false; } } else { return false; } }
/** * @see Object#equals(Object) */ @Override public boolean equals(Object other) { if (other instanceof ValueImpl) { ValueImpl that = (ValueImpl) other; Type<?> thisType = this.type; if (thisType.isArray()) { thisType = thisType.getBaseType(); } Type<?> thatType = that.type; if (thatType.isArray()) { thatType = thatType.getBaseType(); } try { return thisType == thatType && Objects.equal( getValue(thatType, index), that.getValue(thatType, that.index)); } catch (RepositoryException e) { LOG.warn("Error while comparing values", e); return false; } } else { return false; } }
@Test(expected = IllegalStateException.class) public void testBaseTypeOnSingleValueType() { single.getBaseType(); }
/** * @throws IllegalArgumentException if {@code type} is not one of the * values defined in {@link Type}. */ @SuppressWarnings("unchecked") @Nonnull @Override public <S> S getValue(Type<S> type) { if (type.isArray()) { if (getType() == type.getBaseType()) { return (S) singleton(getValue()); } else { return (S) singleton(convertTo(type.getBaseType())); } } else { if (getType() == type) { return (S) getValue(); } else { return convertTo(type); } } }
/** * @throws IllegalArgumentException if {@code type} is not one of the * values defined in {@link Type}. */ @SuppressWarnings("unchecked") @NotNull @Override public <S> S getValue(Type<S> type) { if (type.isArray()) { if (getType() == type.getBaseType()) { return (S) singleton(getValue()); } else { return (S) singleton(convertTo(type.getBaseType())); } } else { if (getType() == type) { return (S) getValue(); } else { return convertTo(type); } } }
@NotNull protected PropertyState createMultiState( String oakName, List<Value> values, Type<?> type) throws RepositoryException { if (values.isEmpty()) { Type<?> base = type.getBaseType(); if (base == UNDEFINED) { base = STRING; } return PropertyBuilder.array(base) .setName(oakName).getPropertyState(); } if (type == UNDEFINEDS) { type = Type.fromTag(values.get(0).getType(), true); } if (type == NAMES || type == PATHS) { Type<?> base = type.getBaseType(); List<String> strings = newArrayListWithCapacity(values.size()); for (Value value : values) { strings.add(getOakValue(value, base)); } return createProperty(oakName, strings, type); } else { return createProperty(oakName, values, type.tag()); } }
@Nonnull protected PropertyState createMultiState( String oakName, List<Value> values, Type<?> type) throws RepositoryException { if (values.isEmpty()) { Type<?> base = type.getBaseType(); if (base == UNDEFINED) { base = STRING; } return PropertyBuilder.array(base) .setName(oakName).getPropertyState(); } if (type == UNDEFINEDS) { type = Type.fromTag(values.get(0).getType(), true); } if (type == NAMES || type == PATHS) { Type<?> base = type.getBaseType(); List<String> strings = newArrayListWithCapacity(values.size()); for (Value value : values) { strings.add(getOakValue(value, base)); } return createProperty(oakName, strings, type); } else { return createProperty(oakName, values, type.tag()); } }
@NotNull protected PropertyState createMultiState( String oakName, List<Value> values, Type<?> type) throws RepositoryException { if (values.isEmpty()) { Type<?> base = type.getBaseType(); if (base == UNDEFINED) { base = STRING; } return PropertyBuilder.array(base) .setName(oakName).getPropertyState(); } if (type == UNDEFINEDS) { type = Type.fromTag(values.get(0).getType(), true); } if (type == NAMES || type == PATHS) { Type<?> base = type.getBaseType(); List<String> strings = newArrayListWithCapacity(values.size()); for (Value value : values) { strings.add(getOakValue(value, base)); } return createProperty(oakName, strings, type); } else { return createProperty(oakName, values, type.tag()); } }
@Override @NotNull @SuppressWarnings("unchecked") public <T> T getValue(Type<T> type) { Segment segment = getSegment(); if (isArray()) { checkState(type.isArray()); ListRecord values = getValueList(segment); if (values.size() == 0) { return (T) emptyList(); } else if (values.size() == 1) { return (T) singletonList(getValue(values.getEntry(0), type.getBaseType())); } else { Type<?> base = type.getBaseType(); List<Object> list = newArrayListWithCapacity(values.size()); for (RecordId id : values.getEntries()) { list.add(getValue(id, base)); } return (T) list; } } else { RecordId id = getRecordId(); if (type.isArray()) { return (T) singletonList( getValue(id, type.getBaseType())); } else { return getValue(id, type); } } }
@Test public void testBaseTypeOnMultiValueType() { assertEquals(single, multi.getBaseType()); }
name, values.getValue(type), type); } else if (values.count() > 0) { type = type.getBaseType(); return PropertyStates.createProperty( name, values.getValue(type, 0), type);
name, values.getValue(type), type); } else if (values.count() > 0) { type = type.getBaseType(); return PropertyStates.createProperty( name, values.getValue(type, 0), type);
@SuppressWarnings("unchecked") private <T> T getValue(RecordId id, Type<T> type) { if (type == BINARY) { return (T) reader.readBlob(id); // load binaries lazily } String value = reader.readString(id); if (type == STRING || type == URI || type == DATE || type == NAME || type == PATH || type == REFERENCE || type == WEAKREFERENCE) { return (T) value; // no conversion needed for string types } Type<?> base = getType(); if (base.isArray()) { base = base.getBaseType(); } Converter converter = Conversions.convert(value, base); if (type == BOOLEAN) { return (T) Boolean.valueOf(converter.toBoolean()); } else if (type == DECIMAL) { return (T) converter.toDecimal(); } else if (type == DOUBLE) { return (T) Double.valueOf(converter.toDouble()); } else if (type == LONG) { return (T) Long.valueOf(converter.toLong()); } else { throw new UnsupportedOperationException( "Unknown type: " + type); } }
public void serialize(PropertyState property) { Type<?> type = property.getType(); if (!type.isArray()) { serialize(property, type, 0); } else { Type<?> base = type.getBaseType(); int count = property.count(); if (base == STRING || count > 0) { json.array(); for (int i = 0; i < count; i++) { serialize(property, base, i); } json.endArray(); } else { // type-safe encoding of an empty array json.value(TypeCodes.EMPTY_ARRAY + PropertyType.nameFromValue(type.tag())); } } }
public void serialize(PropertyState property) { Type<?> type = property.getType(); if (!type.isArray()) { serialize(property, type, 0); } else { Type<?> base = type.getBaseType(); int count = property.count(); if (base == STRING || count > 0) { json.array(); for (int i = 0; i < count; i++) { serialize(property, base, i); } json.endArray(); } else { // type-safe encoding of an empty array json.value(TypeCodes.EMPTY_ARRAY + PropertyType.nameFromValue(type.tag())); } } }
/** * Parse a property * @param parentId * @param propertyId * @param template * @return */ public PropertyInfo parseProperty(RecordId parentId, RecordId propertyId, PropertyTemplate template) { int size = 0; int count = -1; // -1 -> single valued property Segment segment = propertyId.getSegment(); Type<?> type = template.getType(); if (type.isArray()) { count = segment.readInt(propertyId.getRecordNumber()); size += 4; if (count > 0) { RecordId listId = segment.readRecordId(propertyId.getRecordNumber(), 4); size += RECORD_ID_BYTES; for (RecordId valueId : new ListRecord(listId, count).getEntries()) { onValue(propertyId, valueId, type.getBaseType()); } onList(propertyId, listId, count); } } else { onValue(parentId, propertyId, type); } return new PropertyInfo(propertyId, count, size); }