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