diff options
Diffstat (limited to 'bcprov/src/main/java/org/bouncycastle/util')
8 files changed, 166 insertions, 14 deletions
diff --git a/bcprov/src/main/java/org/bouncycastle/util/Arrays.java b/bcprov/src/main/java/org/bouncycastle/util/Arrays.java index 64ce17c..3c0646a 100644 --- a/bcprov/src/main/java/org/bouncycastle/util/Arrays.java +++ b/bcprov/src/main/java/org/bouncycastle/util/Arrays.java @@ -1,6 +1,7 @@ package org.bouncycastle.util; import java.math.BigInteger; +import java.util.Iterator; /** * General array utilities. @@ -968,4 +969,43 @@ public final class Arrays return result; } + + /** + * Iterator backed by a specific array. + */ + public static class Iterator<T> + implements java.util.Iterator<T> + { + private final T[] dataArray; + + private int position = 0; + + /** + * Base constructor. + * <p> + * Note: the array is not cloned, changes to it will affect the values returned by next(). + * </p> + * + * @param dataArray array backing the iterator. + */ + public Iterator(T[] dataArray) + { + this.dataArray = dataArray; + } + + public boolean hasNext() + { + return position < dataArray.length; + } + + public T next() + { + return dataArray[position++]; + } + + public void remove() + { + throw new UnsupportedOperationException("Cannot remove element from an Array."); + } + } } diff --git a/bcprov/src/main/java/org/bouncycastle/util/CollectionStore.java b/bcprov/src/main/java/org/bouncycastle/util/CollectionStore.java index 91aba14..1cc9641 100644 --- a/bcprov/src/main/java/org/bouncycastle/util/CollectionStore.java +++ b/bcprov/src/main/java/org/bouncycastle/util/CollectionStore.java @@ -8,10 +8,10 @@ import java.util.List; /** * A simple collection backed store. */ -public class CollectionStore - implements Store +public class CollectionStore<T> + implements Store<T>, Iterable<T> { - private Collection _local; + private Collection<T> _local; /** * Basic constructor. @@ -19,9 +19,9 @@ public class CollectionStore * @param collection - initial contents for the store, this is copied. */ public CollectionStore( - Collection collection) + Collection<T> collection) { - _local = new ArrayList(collection); + _local = new ArrayList<T>(collection); } /** @@ -30,20 +30,20 @@ public class CollectionStore * @param selector the selector to match against. * @return a possibly empty collection of matching objects. */ - public Collection getMatches(Selector selector) + public Collection<T> getMatches(Selector<T> selector) { if (selector == null) { - return new ArrayList(_local); + return new ArrayList<T>(_local); } else { - List col = new ArrayList(); - Iterator iter = _local.iterator(); + List<T> col = new ArrayList<T>(); + Iterator<T> iter = _local.iterator(); while (iter.hasNext()) { - Object obj = iter.next(); + T obj = iter.next(); if (selector.match(obj)) { @@ -54,4 +54,9 @@ public class CollectionStore return col; } } + + public Iterator<T> iterator() + { + return getMatches(null).iterator(); + } } diff --git a/bcprov/src/main/java/org/bouncycastle/util/Encodable.java b/bcprov/src/main/java/org/bouncycastle/util/Encodable.java new file mode 100644 index 0000000..67258fb --- /dev/null +++ b/bcprov/src/main/java/org/bouncycastle/util/Encodable.java @@ -0,0 +1,18 @@ +package org.bouncycastle.util; + +import java.io.IOException; + +/** + * Interface implemented by objects that can be converted into byte arrays. + */ +public interface Encodable +{ + /** + * Return a byte array representing the implementing object. + * + * @return a byte array representing the encoding. + * @throws java.io.IOException if an issue arises generation the encoding. + */ + byte[] getEncoded() + throws IOException; +} diff --git a/bcprov/src/main/java/org/bouncycastle/util/Iterable.java b/bcprov/src/main/java/org/bouncycastle/util/Iterable.java new file mode 100644 index 0000000..a0fa9dc --- /dev/null +++ b/bcprov/src/main/java/org/bouncycastle/util/Iterable.java @@ -0,0 +1,17 @@ +package org.bouncycastle.util; + +import java.util.Iterator; + +/** + * Utility class to allow use of Iterable feature in JDK 1.5+ + */ +public interface Iterable<T> + extends java.lang.Iterable<T> +{ + /** + * Returns an iterator over a set of elements of type T. + * + * @return an Iterator. + */ + Iterator<T> iterator(); +} diff --git a/bcprov/src/main/java/org/bouncycastle/util/Selector.java b/bcprov/src/main/java/org/bouncycastle/util/Selector.java index 7ad86bf..a3a5ec8 100644 --- a/bcprov/src/main/java/org/bouncycastle/util/Selector.java +++ b/bcprov/src/main/java/org/bouncycastle/util/Selector.java @@ -1,9 +1,9 @@ package org.bouncycastle.util; -public interface Selector +public interface Selector<T> extends Cloneable { - boolean match(Object obj); + boolean match(T obj); Object clone(); } diff --git a/bcprov/src/main/java/org/bouncycastle/util/Store.java b/bcprov/src/main/java/org/bouncycastle/util/Store.java index b994c92..fd28b83 100644 --- a/bcprov/src/main/java/org/bouncycastle/util/Store.java +++ b/bcprov/src/main/java/org/bouncycastle/util/Store.java @@ -2,8 +2,8 @@ package org.bouncycastle.util; import java.util.Collection; -public interface Store +public interface Store<T> { - Collection getMatches(Selector selector) + Collection<T> getMatches(Selector<T> selector) throws StoreException; } diff --git a/bcprov/src/main/java/org/bouncycastle/util/StringList.java b/bcprov/src/main/java/org/bouncycastle/util/StringList.java new file mode 100644 index 0000000..e733442 --- /dev/null +++ b/bcprov/src/main/java/org/bouncycastle/util/StringList.java @@ -0,0 +1,22 @@ +package org.bouncycastle.util; + +public interface StringList + extends Iterable<String> +{ + boolean add(String s); + + String get(int index); + + int size(); + + String[] toStringArray(); + + /** + * Return a section of the contents of the list. If the list is too short the array is filled with nulls. + * + * @param from the initial index of the range to be copied, inclusive + * @param to the final index of the range to be copied, exclusive. + * @return an array of length to - from + */ + String[] toStringArray(int from, int to); +} diff --git a/bcprov/src/main/java/org/bouncycastle/util/Strings.java b/bcprov/src/main/java/org/bouncycastle/util/Strings.java index 770edaa..44ff3ae 100644 --- a/bcprov/src/main/java/org/bouncycastle/util/Strings.java +++ b/bcprov/src/main/java/org/bouncycastle/util/Strings.java @@ -3,6 +3,7 @@ package org.bouncycastle.util; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.OutputStream; +import java.util.ArrayList; import java.util.Vector; public final class Strings @@ -311,4 +312,53 @@ public final class Strings } return res; } + + public static StringList newList() + { + return new StringListImpl(); + } + + private static class StringListImpl + extends ArrayList<String> + implements StringList + { + public boolean add(String s) + { + return super.add(s); + } + + public String set(int index, String element) + { + return super.set(index, element); + } + + public void add(int index, String element) + { + super.add(index, element); + } + + public String[] toStringArray() + { + String[] strs = new String[this.size()]; + + for (int i = 0; i != strs.length; i++) + { + strs[i] = this.get(i); + } + + return strs; + } + + public String[] toStringArray(int from, int to) + { + String[] strs = new String[to - from]; + + for (int i = from; i != this.size() && i != to; i++) + { + strs[i - from] = this.get(i); + } + + return strs; + } + } } |