First introduce the Guava package (a tool class for code validation):
<Dependency> <groupId>Com.google.guava</groupId> <Artifactid>Guava</Artifactid> <version>19.0</version></Dependency>
First step: Establish an interface for index enumeration
Packagecom.enums;Importcom.google.common.base.Preconditions;ImportOrg.slf4j.Logger;Importorg.slf4j.LoggerFactory;Importjava.util.ArrayList;Importjava.util.List;/*** Index enumeration interface starting from 0 * <p/> * Implementing enumeration of this interface the index of each element can be discontinuous <br/> * <b> suggested:</b> * This interface implements the elements of the class to keep the index contiguous to save space * <p/>*/ Public InterfaceIndexedenum {/*** Gets the index value of the enumeration element * *@returnreturns the index value of the >=0*/ Public Abstract intGetIndex (); /*** Tool classes that support Indexable enumeration*/ Public Static Final classIndexedenumutil {PrivateIndexedenumutil () {}Private Static FinalLogger Logger = Loggerfactory.getlogger (indexedenumutil.class); /*** Index warning upper limit, index exceeding this limit there may be a space waste problem*/ Private Static Final intWarnning_max_index = 1000; /*** Place an enumeration element in ArrayList, the sequence of each element in the list is the principle of index<br/> * if there is a discontinuous index, the position in the list is filled by null * * @paramEnums Enumeration Element Array *@param<E> Enum Generics *@returnlist containing all the array elements*/ Public Static<eextendsIndexedenum> list<e>toindexes (e[] enums) {preconditions.checkstate (enums!=NULL&& enums.length > 0), "The Array of enum[%s] cannot be null or empty.", Enums.getclass (). Getcomponenttype (). GetName ()); intMaxindex =Integer.min_value; intCURIDX = 0; //gets the value of the maximum index, and the result of this value +1 is the size of the target container for(E enm:enums) {curidx=Enm.getindex (); Preconditions.checkargument (Curidx>= 0, "The index of enum[%s] must be >= 0.", ENM); if(Curidx >Maxindex) {Maxindex=Curidx; } } if(Maxindex >=Warnning_max_index) {Logger.warn ("The Index of enum[{}] exceed threshold:{}. There is wasting memory probably. ", Enums.getclass (). Getcomponenttype (). GetName (), warnning_max_index); } List<E> instances =NewArraylist<e> (Maxindex + 1); //containers take up null padding for(inti = 0; I < Maxindex + 1; i++) {Instances.add (NULL); } for(E enm:enums) {curidx=Enm.getindex (); //index must be uniquePreconditions.checkstate (Instances.get (curidx) = =NULL, "The index of enum[%s] is not unique.", Enums.getclass (). Getcomponenttype (). GetName ()); Instances.set (Curidx, ENM); } returninstances; } /*** Gets the corresponding enumeration element from the enumeration list according to the specified sequence * <p/> * <b> NOTE: If the index parameter is less than 0 or greater than the maximum index value of the enumeration element, return null</b> * * @paramValues Enumeration List *@paramindex Target sequence *@param<E> Enum Generics *@return */ Public Static<eextendsIndexedenum> E valueOf (list<e> values,intindex) { if(Index < 0 | | | Index >=values.size ()) { return NULL; } returnValues.get (index); } }}
Step two: Set up exception information storage enumeration class
Packagecom.enums;Importjava.util.List;/*** Exception Enumeration*/ Public enumExceptionenumImplementsindexedenum {ex_login (0, "Login exception"), Ex_down (1, "Download Exception"); Private Static FinalList<exceptionenum> Indexs =indexedenumutil.toindexes (Exceptionenum.values ()); /*** Index*/ Private intindex; /*** Pinyin*/ PrivateString message; Exceptionenum (intindex, String message) { This. index =index; This. Message =message; } @Override Public intGetIndex () {returnindex; } PublicString getMessage () {returnmessage; } /*** Get the specified form name according to index * *@paramIndex *@return */ Public StaticExceptionenum IndexOf (Final intindex) { returnindexedenumutil.valueof (indexs, index); }}
An index implementation of Java enumeration storage