Example usage for java.util Enumeration Enumeration

List of usage examples for java.util Enumeration Enumeration

Introduction

In this page you can find the example usage for java.util Enumeration Enumeration.

Prototype

Enumeration

Source Link

Usage

From source file:Main.java

public static <E> Enumeration<E> asEnumeration(final Iterator<E> iter) {
    return new Enumeration<E>() {
        @Override/* w w w . j  av  a 2 s  .  c om*/
        public boolean hasMoreElements() {
            return iter.hasNext();
        }

        @Override
        public E nextElement() {
            return iter.next();
        }
    };
}

From source file:ArrayEnumerationFactory.java

static public Enumeration makeEnumeration(final Object obj) {
    Class type = obj.getClass();//from  w  w  w .j  a v a 2 s  . c o  m
    if (!type.isArray()) {
        throw new IllegalArgumentException(obj.getClass().toString());
    } else {
        return (new Enumeration() {
            int size = Array.getLength(obj);

            int cursor;

            public boolean hasMoreElements() {
                return (cursor < size);
            }

            public Object nextElement() {
                return Array.get(obj, cursor++);
            }
        });
    }
}

From source file:Main.java

public static <T> Enumeration<T> toEnumeration(final Collection<T> collection) {
    return new Enumeration<T>() {

        Object[] array = collection.toArray();
        int index = 0;

        public boolean hasMoreElements() {
            return (index < array.length);
        }/*from   ww w  . j  a v  a  2  s.  c  o  m*/

        @SuppressWarnings("unchecked")
        public T nextElement() {
            return (T) array[index++];
        }
    };
}

From source file:Main.java

/**
 * Returns an iterable as an enumeration (some older library code excepts
 * these)// w w  w  .  j  a v a2 s .c o  m
 * @param iterable the iterable
 * @return an enumeration returning the same sequence of elements as the
 * parameter iterable
 * @precondition iterable != null
 * @postcondition result != null
 */
public static <T> Enumeration<T> asEnumeration(final Iterable<T> iterable) {
    final Iterator<T> it = iterable.iterator();
    return new Enumeration<T>() {
        @Override
        public boolean hasMoreElements() {
            return it.hasNext();
        }

        @Override
        public T nextElement() {
            return it.next();
        }
    };
}

From source file:Main.java

public static <T> Enumeration<T> getEmptyEnumeration() {
    return new Enumeration<T>() {
        public boolean hasMoreElements() {
            return false;
        }//  w w  w.  j a  v a 2 s .c o  m

        public T nextElement() {
            throw new NoSuchElementException("Trying to get element of an empty enumeration");
        }
    };
}

From source file:CollectionUtilities.java

public static Enumeration getIteratorEnumeration(final Iterator i) {
    return new Enumeration() {
        public boolean hasMoreElements() {
            return i.hasNext();
        }//from   ww  w.j av  a 2  s . c  om

        public Object nextElement() {
            return i.next();
        }
    };
}

From source file:org.hippoecm.frontend.model.tree.LabelTreeNode.java

public Enumeration<TreeNode> children() {
    return new Enumeration<TreeNode>() {

        public boolean hasMoreElements() {
            return false;
        }/*from   w  w  w  .j ava  2  s  . c om*/

        public TreeNode nextElement() {
            return null;
        }

    };
}

From source file:therian.operator.convert.EnumerationToListTest.java

private Enumeration<String> tokenize(String s) {
    final StringTokenizer tok = new StringTokenizer(s);
    return new Enumeration<String>() {

        @Override//from   ww w. j a v a  2 s  . c  o  m
        public String nextElement() {
            return tok.nextToken();
        }

        @Override
        public boolean hasMoreElements() {
            return tok.hasMoreElements();
        }
    };
}

From source file:ArrayMap.java

@Override
public Enumeration<String> keys() {
    return new Enumeration<String>() {
        Object nxt;/*from w w  w .j  a v  a 2 s  .  com*/

        Object next;

        Enumeration<String> e;
        {
            e = keyIndex.keys();
            findNext();
        }

        void findNext() {
            next = null;
            while (e.hasMoreElements() && (next = get(nxt = e.nextElement())) == null) {
                ;
            }
        }

        public boolean hasMoreElements() {
            return next != null;
        }

        public String nextElement() {
            Object o = nxt;
            findNext();
            return (String) o;
        }
    };
}

From source file:Main.java

/**
 * Load a given resources. <p/> This method will try to load the resources
 * using the following methods (in order):
 * <ul>//  w  ww.j  a v  a2  s  .c  om
 * <li>From Thread.currentThread().getContextClassLoader()
 * <li>From ClassLoaderUtil.class.getClassLoader()
 * <li>callingClass.getClassLoader()
 * </ul>
 * 
 * @param resourceName The name of the resource to load
 * @param callingClass The Class object of the calling object
 */
public static List<URL> getResources(String resourceName, Class callingClass) {
    List<URL> ret = new ArrayList<URL>();
    Enumeration<URL> urls = new Enumeration<URL>() {
        public boolean hasMoreElements() {
            return false;
        }

        public URL nextElement() {
            return null;
        }

    };
    try {
        urls = Thread.currentThread().getContextClassLoader().getResources(resourceName);
    } catch (IOException e) {
        //ignore
    }
    if (!urls.hasMoreElements() && resourceName.startsWith("/")) {
        //certain classloaders need it without the leading /
        try {
            urls = Thread.currentThread().getContextClassLoader().getResources(resourceName.substring(1));
        } catch (IOException e) {
            // ignore
        }
    }

    ClassLoader cluClassloader = Main.class.getClassLoader();
    if (cluClassloader == null) {
        cluClassloader = ClassLoader.getSystemClassLoader();
    }
    if (!urls.hasMoreElements()) {
        try {
            urls = cluClassloader.getResources(resourceName);
        } catch (IOException e) {
            // ignore
        }
    }
    if (!urls.hasMoreElements() && resourceName.startsWith("/")) {
        //certain classloaders need it without the leading /
        try {
            urls = cluClassloader.getResources(resourceName.substring(1));
        } catch (IOException e) {
            // ignore
        }
    }

    if (!urls.hasMoreElements()) {
        ClassLoader cl = callingClass.getClassLoader();

        if (cl != null) {
            try {
                urls = cl.getResources(resourceName);
            } catch (IOException e) {
                // ignore
            }
        }
    }

    if (!urls.hasMoreElements()) {
        URL url = callingClass.getResource(resourceName);
        if (url != null) {
            ret.add(url);
        }
    }
    while (urls.hasMoreElements()) {
        ret.add(urls.nextElement());
    }

    if (ret.isEmpty() && (resourceName != null) && (resourceName.charAt(0) != '/')) {
        return getResources('/' + resourceName, callingClass);
    }
    return ret;
}