001/*******************************************************************************
002 * This software is provided as a supplement to the authors' textbooks on digital
003 *  image processing published by Springer-Verlag in various languages and editions.
004 * Permission to use and distribute this software is granted under the BSD 2-Clause 
005 * "Simplified" License (see http://opensource.org/licenses/BSD-2-Clause). 
006 * Copyright (c) 2006-2016 Wilhelm Burger, Mark J. Burge. All rights reserved. 
007 * Visit http://imagingbook.com for additional details.
008 *******************************************************************************/
009
010package imagingbook.lib.util;
011
012public abstract class Enums {
013
014        /**
015         * This static method returns an array of all constant names (strings) 
016         * for a given enumeration class.
017         * Assume the enum definition: enum MyEnum {A, B, C};
018         * Usage: String[] names = getEnumNames(MyEnum.class);
019         * 
020         * @param enumclass enumeration class
021         * @return the names defined for the specified enumeration type
022         */
023        public static String[] getEnumNames(Class<? extends Enum<?>> enumclass) {
024                Enum<?>[] eConstants = (Enum<?>[]) enumclass.getEnumConstants();
025                if (eConstants == null) {
026                        return new String[0];
027                }
028                else {
029                        int n = eConstants.length;
030                        String[] eNames = new String[n];
031                        for (int i = 0; i < n; i++) {
032                                eNames[i] = eConstants[i].name();
033                        }
034                        return eNames;
035                }
036        }
037
038}