1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.db.common;
14 import java.util.Arrays;
15 import java.util.Collection;
16 import java.util.Iterator;
18 import org.simantics.db.ReadGraph;
19 import org.simantics.db.Resource;
20 import org.simantics.db.common.utils.NameUtils;
21 import org.simantics.db.exception.DatabaseException;
22 import org.simantics.db.exception.ValidationException;
25 * @author Toni Kalajainen
27 public class ResourceArray implements Iterable<Resource> {
29 public static final ResourceArray[] NONE = new ResourceArray[0];
31 public static final ResourceArray EMPTY = new ResourceArray();
33 public final Resource[] resources;
37 private boolean noNulls(Resource[] rs) {
38 for(Resource r : rs) if(r == null) return false;
42 private boolean noNulls(Collection<Resource> rs) {
43 for(Resource r : rs) if(r == null) return false;
47 public ResourceArray(Resource... resources) {
48 assert(noNulls(resources));
49 this.resources = resources;
50 this.hash = Arrays.hashCode(resources);
53 public ResourceArray(Collection<Resource> resources) {
54 assert(noNulls(resources));
55 this.resources = resources.toArray(new Resource[resources.size()]);
56 this.hash = Arrays.hashCode(this.resources);
60 public int hashCode() {
65 public boolean equals(Object obj) {
68 if (!(obj instanceof ResourceArray))
70 ResourceArray other = (ResourceArray) obj;
71 return Arrays.deepEquals(this.resources, other.resources);
75 public String toString() {
76 StringBuilder sb = new StringBuilder();
78 for (int i = 0; i < resources.length; i++) {
81 sb.append(resources[i].getResourceId());
87 public String toString(ReadGraph g) throws DatabaseException {
88 StringBuilder sb = new StringBuilder();
90 for (int i = 0; i < resources.length; i++) {
93 sb.append(NameUtils.getSafeName(g, resources[i]));
99 public static ResourceArray[] toSingleElementArrays(Resource[] array) {
100 ResourceArray[] result = new ResourceArray[array.length];
101 for (int i = 0; i < result.length; i++)
102 result[i] = new ResourceArray(array[i]);
106 public Resource toSingleResource() throws ValidationException {
107 if(resources.length != 1) throw new ValidationException("Resource array did not contain a single resource (contained " + resources.length + ").");
111 public boolean isEmpty() {
112 return resources.length == 0;
116 return resources.length;
120 * @param i the index of the resource to return
121 * @return ith resource of the array
124 public Resource get(int i) {
129 public Iterator<Resource> iterator() {
130 return Arrays.asList(resources).iterator();
134 * Returns a new ResourceArray instance that contains the resource of this
135 * ResourceArray with the specified resources appended at the end.
137 * @param append the resources to append at the end of the new array
138 * @return a new appended ResourceArray
140 public ResourceArray appended(Resource... append) {
141 if (append.length == 0)
143 Resource[] result = Arrays.copyOf(resources, resources.length + append.length);
144 System.arraycopy(append, 0, result, resources.length, append.length);
145 return new ResourceArray(result);
149 * Returns a new ResourceArray instance that contains the resource of this
150 * ResourceArray with the resources contained in the specified ResourceArray appended at the end.
152 * @param append the ResourceArray to append at the end of the new array
153 * @return a new appended ResourceArray
155 public ResourceArray appended(ResourceArray append) {
156 if (append.size() == 0)
158 Resource[] result = Arrays.copyOf(resources, resources.length + append.size());
159 System.arraycopy(append.resources, 0, result, resources.length, append.size());
160 return new ResourceArray(result);
164 * Returns a new ResourceArray instance that contains the resource of this
165 * ResourceArray with the specified resources prepended at the beginning.
167 * @param prepend the resources to prepend at the beginning of the new array
168 * @return a new prepended ResourceArray
170 public ResourceArray prepended(Resource... prepend) {
171 if (prepend.length == 0)
173 Resource[] result = Arrays.copyOf(prepend, prepend.length + resources.length);
174 System.arraycopy(resources, 0, result, prepend.length, resources.length);
175 return new ResourceArray(result);
181 * @throws IllegalArgumentException
183 public ResourceArray removeFromBeginning(int n) throws IllegalArgumentException {
184 return slice(n, resources.length);
190 * @throws IllegalArgumentException
192 public ResourceArray removeFromEnd(int n) throws IllegalArgumentException {
193 return slice(0, resources.length - n);
196 public ResourceArray slice(int start, int end) {
197 return ((start == 0) && (end == resources.length)) ? this :
198 new ResourceArray(Arrays.copyOfRange(resources, start, end));
201 public ResourceArray reversed() {
202 if (resources.length < 2)
204 Resource[] result = new Resource[resources.length];
205 // for (int i = 0, j = resources.length - 1; i < resources.length / 2; ++i, --j) {
206 // result[j] = resources[i];
207 // result[i] = resources[j];
209 for (int i = 0; i < resources.length ; ++i) {
210 result[i] = resources[resources.length - 1 - i];
212 return new ResourceArray(result);
215 public Resource tail() {
216 return resources[resources.length - 1];
219 public Resource head() {