2 * Copyright (C) 2014, United States Government, as represented by the
3 * Administrator of the National Aeronautics and Space Administration.
6 * The Java Pathfinder core (jpf-core) platform is licensed under the
7 * Apache License, Version 2.0 (the "License"); you may not use this file except
8 * in compliance with the License. You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0.
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 package gov.nasa.jpf.util;
21 import java.lang.reflect.Array;
22 import java.lang.reflect.Constructor;
23 import java.util.ArrayList;
24 import java.util.Collection;
25 import java.util.Comparator;
26 import java.util.HashMap;
27 import java.util.Iterator;
29 import java.util.NoSuchElementException;
32 public static int hashCode(Object o) {
33 return o == null ? 0 : o.hashCode();
36 public static boolean equals(Object a, Object b) {
39 } else if (a == null || b == null) {
40 // only one could be null
47 @SuppressWarnings("unchecked")
48 public static <E> Iterator<E> emptyIterator() {
49 return (Iterator<E>) emptyIterator;
52 @SuppressWarnings("unchecked")
53 public static <E> Iterable<E> emptyIterable() {
54 return (Iterable<E>) emptyIterable;
57 public static <E> Iterable<E> iterableFromIterator(Iterator<E> iter) {
58 return new Iteratorable<E>(iter);
61 public static final Object[] emptyObjectArray = new Object[] {};
63 public static final Iterator<?> emptyIterator = new Iterator<Object>() {
65 public boolean hasNext () { return false; }
67 public Object next () { throw new NoSuchElementException(); }
69 public void remove () { throw new NoSuchElementException(); }
72 public static final Iterable<?> emptyIterable = new Iterable<Object>() {
74 @SuppressWarnings("unchecked")
75 public Iterator<Object> iterator () {
76 return (Iterator<Object>) emptyIterator;
80 @SuppressWarnings("unchecked")
81 public static <B, E extends B> Iterable<B> asBaseIterable (Collection<E> col){
82 Collection<B> base = (Collection)col;
86 @SuppressWarnings("unchecked")
87 public static <B, E extends B> Iterator<B> getBaseIterator (Collection<E> col){
88 Collection<B> base = (Collection)col;
89 return base.iterator();
92 public static <E> void addAll(Collection<E> target, Iterable<? extends E> src) {
93 for (E e : src) target.add(e);
96 public static <T> int indexOf (T[] array, Object elem){
97 for (int i=0; i<array.length; i++){
98 if (array[i].equals(elem)){
106 public static <T> boolean contains (T[] array, Object elem){
107 for (int i=0; i<array.length; i++){
108 if (array[i].equals(elem)){
116 @SuppressWarnings("unchecked")
117 public static <T> T[] stripNullElements(T[] oldArray){
119 for (int i=0; i<oldArray.length; i++){
120 if (oldArray[i] != null){
125 if (count == oldArray.length){ // nothing to strip
129 Class<?> ct = oldArray.getClass().getComponentType();
130 T[] newArray = (T[]) Array.newInstance(ct, count);
133 for (int i=0; i<oldArray.length; i++){
147 @SuppressWarnings("unchecked")
148 public static <T> T[] getAddedElements (T[] oldArray, T[] newArray) {
150 if (newArray == null || newArray.length == 0) {
153 if (oldArray == null || oldArray.length == 0) {
157 T[] na = newArray.clone();
160 for (int i=0; i<na.length; i++) {
163 for (int j=0; j<oldArray.length; j++) {
164 if (eNew.equals(oldArray[j])) {
175 Class<?> ct = newArray.getClass().getComponentType();
176 T[] addedElements = (T[]) Array.newInstance(ct, n);
178 for (int i=0, j=0; i<na.length; i++) {
180 addedElements[j++] = na[i];
184 return addedElements;
187 public static <T> T[] getRemovedElements (T[] oldArray, T[] newArray) {
189 if (newArray == null || newArray.length == 0 || oldArray == null || oldArray.length == 0) {
193 T[] oa = oldArray.clone();
196 for (int i=0; i<oa.length; i++) {
199 for (int j=0; j<newArray.length; j++) {
200 if (eOld.equals(newArray[j])) {
211 Class<?> ct = oldArray.getClass().getComponentType();
212 T[] addedElements = (T[]) Array.newInstance(ct, n);
214 for (int i=0, j=0; i<oa.length; i++) {
216 addedElements[j++] = oa[i];
220 return addedElements;
224 public static <K,V> ArrayList<String> getSortedKeyStrings (HashMap<K,V> map){
225 ArrayList<String> list = new ArrayList<String>();
228 for (K key : map.keySet()){
229 String ks = key.toString();
231 for (int i=0; i<list.size(); i++){
232 String k = list.get(i);
233 if (ks.compareTo(k) > 0){
245 public static <K,V> ArrayList<Map.Entry<K,V>> createSortedEntryList (HashMap<K,V> map,
246 Comparator<Map.Entry<K,V>> comparer) {
247 ArrayList<Map.Entry<K,V>> list = new ArrayList<Map.Entry<K,V>>();
250 for (Map.Entry<K,V> e : map.entrySet()){
252 for (int i=0; i<list.size(); i++){
253 if (comparer.compare(e,list.get(i)) > 0) {
265 public static <K,V,E> ArrayList<E> createSortedList (HashMap<K,V> map,
266 TwoTypeComparator<Map.Entry<K,V>,E> comparer,
267 ElementCreator<Map.Entry<K,V>,E> creator) {
268 ArrayList<E> list = new ArrayList<E>();
271 for (Map.Entry<K,V> e : map.entrySet()){
273 for (int i=0; i<list.size(); i++){
274 if (comparer.compare(e,list.get(i)) > 0) {
275 list.add(i, creator.create(e));
279 list.add(creator.create(e));
286 public static int compare (Integer i1, Integer i2) {
287 return Integer.signum(i1.intValue() - i2.intValue());
290 public static <E,T> HashMap<E,Integer> createOccurrenceMap (Collection<T> collection,
291 ElementCreator<T,E> creator) {
292 HashMap<E,Integer> map = new HashMap<E,Integer>();
294 for (T o : collection) {
295 E e = creator.create(o);
296 Integer nE = map.get(e);
300 map.put(e,nE.intValue()+1);
307 public static <T> T createObject (Class<T> cls, Class<?>[] argTypes, Object[] args){
308 if (argTypes.length != args.length){
312 while (argTypes.length >= 0){
314 Constructor<T> ctor = cls.getConstructor(argTypes);
315 return ctor.newInstance(args);
317 } catch (NoSuchMethodException nsmx){
318 Class<?>[] at = new Class<?>[argTypes.length-1];
319 System.arraycopy(argTypes, 1, at,0, at.length);
322 Object[] a = new Object[at.length];
323 System.arraycopy(args,1, a,0, a.length);
326 } catch (Throwable t){
334 public static String toString (Object[] collection) {
335 StringBuilder sb = new StringBuilder();
337 for (Object e : collection) {
341 return sb.toString();
344 public static String toString (String[] collection) {
345 StringBuilder sb = new StringBuilder();
347 for (Object e : collection) {
351 return sb.toString();
354 public static String toString (Iterable<?> collection) {
355 StringBuilder sb = new StringBuilder();
357 for (Object e : collection) {
361 return sb.toString();
364 public static String toString( Iterable<?> collection,
365 String prefix, String separator, String postfix) {
366 StringBuilder sb = new StringBuilder();
368 if (prefix != null) {
373 for (Object e : collection) {
375 sb.append(separator);
380 if (postfix != null) {
384 return sb.toString();
387 public static String toString( Object[] array,
388 String prefix, String separator, String postfix) {
389 StringBuilder sb = new StringBuilder();
391 if (prefix != null) {
395 for (int i=0; i<array.length; i++) {
397 sb.append(separator);
402 if (postfix != null) {
406 return sb.toString();
409 public static String toString( int[] array, int start, int end, String prefix, String separator, String postfix){
410 StringBuilder sb = new StringBuilder();
412 if (prefix != null) {
416 for (int i=start; i<array.length && i < end; i++) {
418 sb.append(separator);
423 if (postfix != null) {
427 return sb.toString();
430 public static <T> T[] newArray (T... elements) {
434 public static <T> T[] appendArray (T[] base, T... elements) {
435 if (base == null || base.length == 0){
437 } else if (elements == null || elements.length == 0){
440 Class<?> componentType = base.getClass().getComponentType();
441 T[] a = (T[]) Array.newInstance(componentType, base.length + elements.length);
442 System.arraycopy(base, 0, a, 0, base.length);
443 System.arraycopy(elements, 0, a, base.length, elements.length);
448 public static <T> T[] prependArray (T[] base, T... elements) {
449 if (base == null || base.length == 0){
451 } else if (elements == null || elements.length == 0){
454 Class<?> componentType = base.getClass().getComponentType();
455 T[] a = (T[]) Array.newInstance(componentType, base.length + elements.length);
456 System.arraycopy(base,0, a,elements.length, base.length);
457 System.arraycopy(elements,0, a,0, elements.length);
462 public static String[] prependArray (String[] base, String... elements){
463 if (base == null || base.length == 0){
465 } else if (elements == null || elements.length == 0){
468 String[] a = new String[base.length + elements.length];
469 System.arraycopy(base,0, a,elements.length, base.length);
470 System.arraycopy(elements,0, a,0, elements.length);
476 public static <T> T[] arrayWithoutFirst( T[] base, int nElements){
479 } else if (nElements >= base.length){
480 Class<?> componentType = base.getClass().getComponentType();
481 T[] a = (T[]) Array.newInstance(componentType, 0);
484 int n = base.length - nElements;
485 Class<?> componentType = base.getClass().getComponentType();
486 T[] a = (T[]) Array.newInstance(componentType, n);
487 System.arraycopy(base, nElements, a, 0, n);
492 public static <T> T[] appendElement (T[] base, T newElement){
493 int len = base.length;
495 Class<?> componentType = base.getClass().getComponentType();
496 T[] a = (T[]) Array.newInstance(componentType, len + 1);
497 System.arraycopy(base, 0, a, 0, len);
503 public static <T> T[] appendUniqueElement (T[] base, T newElement){
504 int len = base.length;
506 for (int i=0; i<len; i++){
507 if (base[i] == newElement){
512 return appendElement(base, newElement);
516 public static <T> T[] removeElement (T[] base, T element) {
517 int len = base.length;
519 for (int i=0; i<len; i++){
520 if (base[i] == element){
521 Class<?> componentType = base.getClass().getComponentType();
522 T[] a = (T[]) Array.newInstance(componentType, len -1);
523 System.arraycopy(base, 0, a, 0, i);
524 System.arraycopy(base, i+1, a, i, len-i-1);
532 public static <T> boolean hasElementOfType (T[] base, Class<?> elemType){
533 int len = base.length;
534 for (int i=0; i<len; i++){
535 if (elemType.isInstance(base[i])){
543 public static <T,E> E getNextElementOfType (T[] base, Class<E> elemType, T prev){
544 boolean prevSeen = (prev == null);
545 int len = base.length;
546 for (int i=0; i<len; i++){
547 if (elemType.isInstance(base[i])){
551 prevSeen = (base[i] == prev);
559 public static String[] arrayWithoutFirst(String[] base, int nElements){
560 String[] a = new String[base.length-1];
562 System.arraycopy(base,1,a,0,a.length);
567 public static boolean equals (Object[] a1, Object[] a2){
574 for (int i=0; i<a2.length; i++){
580 } else if (a2 == null){
581 for (int i=0; i<a1.length; i++){
587 if (a1.length != a2.length){
591 for (int i = 0; i < a1.length; i++) {
596 if (!o1.equals(o2)) {
599 } else if (o2 != null) {
610 * equals first len objects of two reference arrays, which can contain null
611 * elements. If any of the reference arrays is null, this is treated as
612 * if all elements would be null
614 public static boolean compare (int len, Object[] a1, Object[] a2){
615 if (a1 == null && a2 == null){
621 if (a2.length < len){
624 for (int i=0; i<len; i++){
630 } else if (a2 == null){
631 if (a1.length < len){
634 for (int i=0; i<len; i++){
640 if (a1.length < len || a2.length < len){
644 for (int i = 0; i < len; i++) {
649 if (!o1.equals(o2)) {
652 } else if (o2 != null) {
661 public static String stripToLastDot (String s){
662 int i = s.lastIndexOf('.');
664 return s.substring(i+1);
670 public static int setBit (int val, int idx){
671 return (val | (1<<idx));
674 public static int clearBit (int val, int idx){
675 return (val & ~(1<<idx));
678 public static String upToFirst( String s, char c){
679 int i = s.indexOf(c);
681 return s.substring(0, i);
687 public static String fromFirst( String s, char c){
688 int i = s.indexOf(c);
690 return s.substring(i);
697 /*=================== PRIVATE STUFF ===================*/
699 private static final class Iteratorable<E> implements Iterable<E> {
702 public Iteratorable(Iterator<E> iter) {
707 public Iterator<E> iterator () {
708 Iterator<E> ret = iter;