All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.landawn.abacus.util.Enumerations Maven / Gradle / Ivy

Go to download

A general programming library in Java/Android. It's easy to learn and simple to use with concise and powerful APIs.

There is a newer version: 5.2.4
Show newest version
/*
 * Copyright (c) 2020, Haiyang Li.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.landawn.abacus.util;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.Set;
import java.util.function.Supplier;

public final class Enumerations {

    @SuppressWarnings("rawtypes")
    private static final Enumeration EMPTY = new Enumeration() {
        @Override
        public boolean hasMoreElements() {
            return false;
        }

        @Override
        public Object nextElement() {
            throw new NoSuchElementException();
        }
    };

    private Enumerations() {
        // singleton
    }

    /**
     * 
     *
     * @param  
     * @return 
     */
    public static  Enumeration empty() {
        return EMPTY;
    }

    /**
     * 
     *
     * @param  
     * @param single 
     * @return 
     */
    public static  Enumeration just(final T single) {
        return new Enumeration<>() {
            private boolean hasNext = true;

            @Override
            public boolean hasMoreElements() {
                return hasNext;
            }

            @Override
            public T nextElement() {
                if (!hasNext) {
                    throw new NoSuchElementException();
                }

                hasNext = false;

                return single;
            }
        };
    }

    /**
     * 
     *
     * @param  
     * @param a 
     * @return 
     */
    public static  Enumeration of(final T... a) {
        if (N.isNullOrEmpty(a)) {
            return empty();
        }

        return new Enumeration<>() {
            private final int len = a.length;
            private int cursor = 0;

            @Override
            public boolean hasMoreElements() {
                return cursor < len;
            }

            @Override
            public T nextElement() {
                if (cursor >= len) {
                    throw new NoSuchElementException();
                }

                return a[cursor++];
            }
        };
    }

    /**
     * 
     *
     * @param  
     * @param c 
     * @return 
     */
    public static  Enumeration from(final Collection c) {
        if (N.isNullOrEmpty(c)) {
            return empty();
        }

        return from(c.iterator());
    }

    /**
     * 
     *
     * @param  
     * @param iter 
     * @return 
     */
    public static  Enumeration from(final Iterator iter) {
        return new Enumeration<>() {
            @Override
            public boolean hasMoreElements() {
                return iter.hasNext();
            }

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

    /**
     *
     * @param 
     * @param a
     * @return
     */
    @SafeVarargs
    public static  Enumeration concat(final Enumeration... a) {
        if (N.isNullOrEmpty(a)) {
            return empty();
        }

        return concat(Array.asList(a));
    }

    /**
     *
     * @param 
     * @param c
     * @return
     */
    public static  Enumeration concat(final Collection> c) {
        if (N.isNullOrEmpty(c)) {
            return empty();
        }

        return new Enumeration<>() {
            private final Iterator> iter = c.iterator();
            private Enumeration cur;

            @Override
            public boolean hasMoreElements() {
                while ((cur == null || !cur.hasMoreElements()) && iter.hasNext()) {
                    cur = iter.next();
                }

                return cur != null && cur.hasMoreElements();
            }

            @Override
            public T nextElement() {
                if ((cur == null || !cur.hasMoreElements()) && !hasMoreElements()) {
                    throw new NoSuchElementException();
                }

                return cur.nextElement();
            }
        };
    }

    /**
     * 
     *
     * @param  
     * @param e 
     * @return 
     */
    public static  ObjIterator toIterator(final Enumeration e) {
        if (e == null) {
            return ObjIterator.empty();
        }

        return new ObjIterator<>() {
            @Override
            public boolean hasNext() {
                return e.hasMoreElements();
            }

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

    /**
     * 
     *
     * @param  
     * @param e 
     * @return 
     */
    public static  List toList(final Enumeration e) {
        if (e == null) {
            return new ArrayList<>();
        }

        final List result = new ArrayList<>();

        while (e.hasMoreElements()) {
            result.add(e.nextElement());
        }

        return result;
    }

    /**
     * 
     *
     * @param  
     * @param e 
     * @return 
     */
    public static  Set toSet(final Enumeration e) {
        if (e == null) {
            return new HashSet<>();
        }

        final Set result = new HashSet<>();

        while (e.hasMoreElements()) {
            result.add(e.nextElement());
        }

        return result;
    }

    /**
     * 
     *
     * @param  
     * @param  
     * @param e 
     * @param supplier 
     * @return 
     */
    public static > C toCollection(final Enumeration e, final Supplier supplier) {
        final C c = supplier.get();

        if (e != null) {
            while (e.hasMoreElements()) {
                c.add(e.nextElement());
            }
        }

        return c;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy