iot.jcypher.query.values.JcCollection Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jcypher Show documentation
Show all versions of jcypher Show documentation
Provides seamlessly integrated Java access to graph databases (Neo4J)
at different levels of abstraction.
The newest version!
/************************************************************************
* Copyright (c) 2014-2016 IoT-Solutions e.U.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
************************************************************************/
package iot.jcypher.query.values;
import java.util.Collection;
import java.util.List;
import iot.jcypher.domainquery.internal.QueryRecorder;
import iot.jcypher.query.values.functions.FUNCTION;
import iot.jcypher.query.values.operators.OPERATOR;
public class JcCollection extends JcValue {
private Object value;
private Class type;
JcCollection() {
super();
}
/**
* JCYPHER
* create a collection which is identified by a name
*
*/
public JcCollection(String name) {
super(name);
}
/**
* JCYPHER
* create a JcCollection initialized with a possibly empty list of primitives.
* This serves to create lists of literals as e.g. [1, 2, 3] or ['Peter', 'Paul', 'Mary']...
*
*/
public JcCollection(List> list) {
this(fromPrimitiveList(list), null, null);
}
JcCollection(String name, ValueElement predecessor, IOperatorOrFunction opf) {
super(name, predecessor, opf);
}
JcCollection(String name, ValueElement predecessor, IOperatorOrFunction opf, Class typ) {
super(name, predecessor, opf);
this.type = typ;
}
JcCollection(String name, Object val, ValueElement predecessor, IOperatorOrFunction opf) {
super(name, predecessor, opf);
this.value = val;
}
/**
* JCYPHER
* return the size of a collection, return a JcNumber
*
*/
public JcNumber length() {
JcNumber ret = new JcNumber(null, this,
new FunctionInstance(FUNCTION.Collection.LENGTH, 1));
QueryRecorder.recordInvocationConditional(this, "length", ret);
return ret;
}
/**
* JCYPHER
* add a value to a collection, return a JcCollection.
* The value can be a simple value or being derived via an expression
* like ...add(a.property("value"));.
*
*/
public JcCollection add(Object value) {
JcCollection ret = new JcCollection(null, value, this,
OPERATOR.Collection.ADD);
QueryRecorder.recordInvocationConditional(this, "add", ret, QueryRecorder.placeHolder(value));
return ret;
}
/**
* JCYPHER
* add all elements of a collection to this collection, return a JcCollection
*
*/
public JcCollection addAll(Collection> coll) {
JcCollection ret = new JcCollection(null, coll, this,
OPERATOR.Collection.ADD_ALL);
QueryRecorder.recordInvocationConditional(this, "addAll", ret, QueryRecorder.literal(coll));
return ret;
}
/**
* JCYPHER
* access the element of the collection at the given index, return a JcProperty
*
*/
@SuppressWarnings("unchecked")
public T get(int index) {
T ret;
if (JcRelation.class.equals(type)) {
ret = (T) new JcRelation(null, this, OPERATOR.Collection.GET);
} else if (JcNode.class.equals(type)) {
ret = (T) new JcNode(null, this, OPERATOR.Collection.GET);
} else {
ret = (T) new JcValue(null, this, OPERATOR.Collection.GET);
}
ret.setHint(ValueAccess.hintKey_opValue, index);
QueryRecorder.recordInvocationConditional(this, "get", ret, QueryRecorder.literal(index));
return ret;
}
/**
* JCYPHER
* access the element of the collection at the index given by the indexExpression, return a JcProperty
* e.g. ...get(a.numberProperty("index"));.
*
*/
@SuppressWarnings("unchecked")
public T get(JcNumber indexExpression) {
T ret;
if (JcRelation.class.equals(type)) {
ret = (T) new JcRelation(null, this, OPERATOR.Collection.GET);
} else if (JcNode.class.equals(type)) {
ret = (T) new JcNode(null, this, OPERATOR.Collection.GET);
} else {
ret = (T) new JcValue(null, this, OPERATOR.Collection.GET);
}
ret.setHint(ValueAccess.hintKey_opValue, indexExpression);
QueryRecorder.recordInvocationConditional(this, "get", ret, QueryRecorder.placeHolder(indexExpression));
return ret;
}
/**
* JCYPHER
* return the first element of a collection
*
*/
@SuppressWarnings("unchecked")
public T head() {
T ret;
if (JcRelation.class.equals(type)) {
ret = (T) new JcRelation(null, this,
new FunctionInstance(FUNCTION.Collection.HEAD, 1));
} else if (JcNode.class.equals(type)) {
ret = (T) new JcNode(null, this,
new FunctionInstance(FUNCTION.Collection.HEAD, 1));
} else {
ret = (T) new JcValue(null, this,
new FunctionInstance(FUNCTION.Collection.HEAD, 1));
}
QueryRecorder.recordInvocationConditional(this, "head", ret);
return ret;
}
/**
* JCYPHER
* return the last element of a collection
*
*/
@SuppressWarnings("unchecked")
public T last() {
T ret;
if (JcRelation.class.equals(type)) {
ret = (T) new JcRelation(null, this,
new FunctionInstance(FUNCTION.Collection.LAST, 1));
} else if (JcNode.class.equals(type)) {
ret = (T) new JcNode(null, this,
new FunctionInstance(FUNCTION.Collection.LAST, 1));
} else {
ret = (T) new JcValue(null, this,
new FunctionInstance(FUNCTION.Collection.LAST, 1));
}
QueryRecorder.recordInvocationConditional(this, "last", ret);
return ret;
}
Object getValue() {
return value;
}
private static String fromPrimitiveList(List> list) {
StringBuilder sb = new StringBuilder();
sb.append('[');
if (list != null) {
int idx = 0;
for (Object val : list) {
if (idx > 0)
sb.append(", ");
if (val instanceof String) {
sb.append('\'');
sb.append(val.toString());
sb.append('\'');
} else
sb.append(val.toString());
idx++;
}
}
sb.append(']');
return sb.toString();
}
}