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