--- /dev/null
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
+ * in Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ * VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.db.common;\r
+\r
+import java.util.Arrays;\r
+import java.util.Collection;\r
+import java.util.Iterator;\r
+\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.common.utils.NameUtils;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.db.exception.ValidationException;\r
+\r
+/**\r
+ * @author Toni Kalajainen\r
+ */\r
+public class ResourceArray implements Iterable<Resource> {\r
+\r
+ public static final ResourceArray[] NONE = new ResourceArray[0];\r
+\r
+ public static final ResourceArray EMPTY = new ResourceArray();\r
+\r
+ public final Resource[] resources;\r
+\r
+ private int hash;\r
+\r
+ private boolean noNulls(Resource[] rs) {\r
+ for(Resource r : rs) if(r == null) return false;\r
+ return true;\r
+ }\r
+ \r
+ private boolean noNulls(Collection<Resource> rs) {\r
+ for(Resource r : rs) if(r == null) return false;\r
+ return true;\r
+ }\r
+\r
+ public ResourceArray(Resource... resources) {\r
+ assert(noNulls(resources));\r
+ this.resources = resources;\r
+ this.hash = Arrays.hashCode(resources);\r
+ }\r
+\r
+ public ResourceArray(Collection<Resource> resources) {\r
+ assert(noNulls(resources));\r
+ this.resources = resources.toArray(new Resource[resources.size()]);\r
+ this.hash = Arrays.hashCode(this.resources);\r
+ }\r
+\r
+ @Override\r
+ public int hashCode() {\r
+ return hash;\r
+ }\r
+\r
+ @Override\r
+ public boolean equals(Object obj) {\r
+ if (this == obj)\r
+ return true;\r
+ if (!(obj instanceof ResourceArray))\r
+ return false;\r
+ ResourceArray other = (ResourceArray) obj;\r
+ return Arrays.deepEquals(this.resources, other.resources);\r
+ }\r
+\r
+ @Override\r
+ public String toString() {\r
+ StringBuilder sb = new StringBuilder();\r
+ sb.append("[");\r
+ for (int i = 0; i < resources.length; i++) {\r
+ if (i > 0)\r
+ sb.append(", ");\r
+ sb.append(resources[i].getResourceId());\r
+ }\r
+ sb.append("]");\r
+ return sb.toString();\r
+ }\r
+\r
+ public String toString(ReadGraph g) throws DatabaseException {\r
+ StringBuilder sb = new StringBuilder();\r
+ sb.append("[");\r
+ for (int i = 0; i < resources.length; i++) {\r
+ if (i > 0)\r
+ sb.append(", ");\r
+ sb.append(NameUtils.getSafeName(g, resources[i]));\r
+ }\r
+ sb.append("]");\r
+ return sb.toString();\r
+ }\r
+\r
+ public static ResourceArray[] toSingleElementArrays(Resource[] array) {\r
+ ResourceArray[] result = new ResourceArray[array.length];\r
+ for (int i = 0; i < result.length; i++)\r
+ result[i] = new ResourceArray(array[i]);\r
+ return result;\r
+ }\r
+ \r
+ public Resource toSingleResource() throws ValidationException {\r
+ if(resources.length != 1) throw new ValidationException("Resource array did not contain a single resource (contained " + resources.length + ").");\r
+ return resources[0];\r
+ }\r
+\r
+ public boolean isEmpty() {\r
+ return resources.length == 0;\r
+ }\r
+\r
+ public int size() {\r
+ return resources.length;\r
+ }\r
+\r
+ /**\r
+ * @param i the index of the resource to return\r
+ * @return ith resource of the array\r
+ * @since 1.6\r
+ */\r
+ public Resource get(int i) {\r
+ return resources[i];\r
+ }\r
+\r
+ @Override\r
+ public Iterator<Resource> iterator() {\r
+ return Arrays.asList(resources).iterator();\r
+ }\r
+\r
+ /**\r
+ * Returns a new ResourceArray instance that contains the resource of this\r
+ * ResourceArray with the specified resources appended at the end.\r
+ * \r
+ * @param append the resources to append at the end of the new array\r
+ * @return a new appended ResourceArray\r
+ */\r
+ public ResourceArray appended(Resource... append) {\r
+ if (append.length == 0)\r
+ return this;\r
+ Resource[] result = Arrays.copyOf(resources, resources.length + append.length);\r
+ System.arraycopy(append, 0, result, resources.length, append.length);\r
+ return new ResourceArray(result);\r
+ }\r
+\r
+ /**\r
+ * Returns a new ResourceArray instance that contains the resource of this\r
+ * ResourceArray with the resources contained in the specified ResourceArray appended at the end.\r
+ * \r
+ * @param append the ResourceArray to append at the end of the new array\r
+ * @return a new appended ResourceArray\r
+ */\r
+ public ResourceArray appended(ResourceArray append) {\r
+ if (append.size() == 0)\r
+ return this;\r
+ Resource[] result = Arrays.copyOf(resources, resources.length + append.size());\r
+ System.arraycopy(append.resources, 0, result, resources.length, append.size());\r
+ return new ResourceArray(result);\r
+ }\r
+ \r
+ /**\r
+ * Returns a new ResourceArray instance that contains the resource of this\r
+ * ResourceArray with the specified resources prepended at the beginning.\r
+ * \r
+ * @param prepend the resources to prepend at the beginning of the new array\r
+ * @return a new prepended ResourceArray\r
+ */\r
+ public ResourceArray prepended(Resource... prepend) {\r
+ if (prepend.length == 0)\r
+ return this;\r
+ Resource[] result = Arrays.copyOf(prepend, prepend.length + resources.length);\r
+ System.arraycopy(resources, 0, result, prepend.length, resources.length);\r
+ return new ResourceArray(result);\r
+ }\r
+\r
+ /**\r
+ * @param n\r
+ * @return\r
+ * @throws IllegalArgumentException\r
+ */\r
+ public ResourceArray removeFromBeginning(int n) throws IllegalArgumentException {\r
+ return slice(n, resources.length);\r
+ }\r
+\r
+ /**\r
+ * @param n\r
+ * @return\r
+ * @throws IllegalArgumentException\r
+ */\r
+ public ResourceArray removeFromEnd(int n) throws IllegalArgumentException {\r
+ return slice(0, resources.length - n);\r
+ }\r
+\r
+ public ResourceArray slice(int start, int end) {\r
+ return ((start == 0) && (end == resources.length)) ? this :\r
+ new ResourceArray(Arrays.copyOfRange(resources, start, end));\r
+ }\r
+\r
+ public ResourceArray reversed() {\r
+ if (resources.length < 2)\r
+ return this;\r
+ Resource[] result = new Resource[resources.length];\r
+// for (int i = 0, j = resources.length - 1; i < resources.length / 2; ++i, --j) {\r
+// result[j] = resources[i];\r
+// result[i] = resources[j];\r
+// }\r
+ for (int i = 0; i < resources.length ; ++i) {\r
+ result[i] = resources[resources.length - 1 - i];\r
+ }\r
+ return new ResourceArray(result);\r
+ }\r
+ \r
+ public Resource tail() {\r
+ return resources[resources.length - 1];\r
+ }\r
+\r
+ public Resource head() {\r
+ return resources[0];\r
+ }\r
+ \r
+}\r