final Optional<Object> persistedValue() { return value().map(value -> { // Convert values to how they are stored in the graph DataType dataType = DataType.SUPPORTED_TYPES.get(value.getClass().getName()); if (dataType == null) { throw GraqlQueryException.invalidValueClass(value); } // We can trust the `SUPPORTED_TYPES` map to store things with the right type //noinspection unchecked return dataType.getPersistenceValue(value); }); }
/** * This is to handle casting longs and doubles when the type allows for the data type to be a number * @param value The value of the resource * @return The value casted to the correct type */ private static Object castValue(AttributeType.DataType dataType, Object value){ try { if (dataType.equals(AttributeType.DataType.DOUBLE)) { return ((Number) value).doubleValue(); } else if (dataType.equals(AttributeType.DataType.LONG)) { if (value instanceof Double) { throw new ClassCastException(); } return ((Number) value).longValue(); } else if (dataType.equals(AttributeType.DataType.DATE) && (value instanceof Long)){ return value; } else { return dataType.getPersistenceValue(value); } } catch (ClassCastException e) { throw GraknTxOperationException.invalidAttributeValue(value, dataType); } }
@Override public <V> Collection<Attribute<V>> getAttributesByValue(V value) { if (value == null) return Collections.emptySet(); //Make sure you trying to retrieve supported data type if (!AttributeType.DataType.SUPPORTED_TYPES.containsKey(value.getClass().getName())) { throw GraknTxOperationException.unsupportedDataType(value); } HashSet<Attribute<V>> attributes = new HashSet<>(); AttributeType.DataType dataType = AttributeType.DataType.SUPPORTED_TYPES.get(value.getClass().getTypeName()); //noinspection unchecked getConcepts(dataType.getVertexProperty(), dataType.getPersistenceValue(value)).forEach(concept -> { if (concept != null && concept.isAttribute()) { //noinspection unchecked attributes.add(concept.asAttribute()); } }); return attributes; }