阅读EnumSet抽象类源码
EnumSet
EnumSet是Java枚举类型的泛型容器,Java既然有了SortedSet、TreeSet、HashSet等容器,为何还要多一个EnumSet
专门为枚举类设计的集合类,所有元素必须是枚举类型
EnumSet的集合元素是有序的,内部以位向量的形成存储,因此占用内存小,效率高
不允许加入null元素
源码
packagejava.util; importsun.misc.SharedSecrets; publicabstractclassEnumSet>extendsAbstractSet implementsCloneable,java.io.Serializable { /** *元素类型 */ finalClass elementType; /** *通过数组存储元素 */ finalEnum[]universe; privatestaticEnum[]ZERO_LENGTH_ENUM_ARRAY=newEnum[0]; EnumSet(Class elementType,Enum[]universe){ this.elementType=elementType; this.universe=universe; } /** *创造一个空的enumset并制定其元素类型 *@paramelementTypetheclassobjectoftheelementtypeforthisenum *set *@throwsNullPointerExceptionifelementTypeisnull */ publicstatic >EnumSet noneOf(Class elementType){ Enum[]universe=getUniverse(elementType); if(universe==null) thrownewClassCastException(elementType+"notanenum"); if(universe.length<=64) returnnewRegularEnumSet<>(elementType,universe); else returnnewJumboEnumSet<>(elementType,universe); } /** *创建一个包含所有在指定元素类型的元素的枚举set * *@paramelementTypetheclassobjectoftheelementtypeforthisenum *set *@throwsNullPointerExceptionifelementTypeisnull */ publicstatic >EnumSet allOf(Class elementType){ EnumSet result=noneOf(elementType); result.addAll(); returnresult; } /** *Addsalloftheelementsfromtheappropriateenumtypetothisenum *set,whichisemptypriortothecall. */ abstractvoidaddAll(); /** *创建一个枚举设置相同的元素类型与指定枚举set * *@paramstheenumsetfromwhichtoinitializethisenumset *@throwsNullPointerExceptionifsisnull */ publicstatic >EnumSet copyOf(EnumSet s){ returns.clone(); } /** *创建一个枚举集从指定集合初始化,最初包含相同的元素 *@paramcthecollectionfromwhichtoinitializethisenumset *@throwsIllegalArgumentExceptionifcisnotan *EnumSetinstanceandcontainsnoelements *@throwsNullPointerExceptionifcisnull */ publicstatic >EnumSet copyOf(Collection c){ if(cinstanceofEnumSet){ return((EnumSet )c).clone(); }else{ if(c.isEmpty()) thrownewIllegalArgumentException("Collectionisempty"); Iterator i=c.iterator(); Efirst=i.next(); EnumSet result=EnumSet.of(first); while(i.hasNext()) result.add(i.next()); returnresult; } } /** *创建一个枚举集合,其元素与s相同 *@paramstheenumsetfromwhosecomplementtoinitializethisenumset *@throwsNullPointerExceptionifsisnull */ publicstatic >EnumSet complementOf(EnumSet s){ EnumSet result=copyOf(s); result.complement(); returnresult; } /** *1个元素枚举集合 * *@parametheelementthatthissetistocontaininitially *@throwsNullPointerExceptionifeisnull *@returnanenumsetinitiallycontainingthespecifiedelement */ publicstatic >EnumSet of(Ee){ EnumSet result=noneOf(e.getDeclaringClass()); result.add(e); returnresult; } /** *2个元素枚举集合 * *@parame1anelementthatthissetistocontaininitially *@parame2anotherelementthatthissetistocontaininitially *@throwsNullPointerExceptionifanyparametersarenull *@returnanenumsetinitiallycontainingthespecifiedelements */ publicstatic >EnumSet of(Ee1,Ee2){ EnumSet result=noneOf(e1.getDeclaringClass()); result.add(e1); result.add(e2); returnresult; } /** *3个元素枚举集合 * *@parame1anelementthatthissetistocontaininitially *@parame2anotherelementthatthissetistocontaininitially *@parame3anotherelementthatthissetistocontaininitially *@throwsNullPointerExceptionifanyparametersarenull *@returnanenumsetinitiallycontainingthespecifiedelements */ publicstatic >EnumSet of(Ee1,Ee2,Ee3){ EnumSet result=noneOf(e1.getDeclaringClass()); result.add(e1); result.add(e2); result.add(e3); returnresult; } /** *4个元素枚举集合 *@parame1anelementthatthissetistocontaininitially *@parame2anotherelementthatthissetistocontaininitially *@parame3anotherelementthatthissetistocontaininitially *@parame4anotherelementthatthissetistocontaininitially *@throwsNullPointerExceptionifanyparametersarenull *@returnanenumsetinitiallycontainingthespecifiedelements */ publicstatic >EnumSet of(Ee1,Ee2,Ee3,Ee4){ EnumSet result=noneOf(e1.getDeclaringClass()); result.add(e1); result.add(e2); result.add(e3); result.add(e4); returnresult; } /** *5个元素枚举集合 * *@parame1anelementthatthissetistocontaininitially *@parame2anotherelementthatthissetistocontaininitially *@parame3anotherelementthatthissetistocontaininitially *@parame4anotherelementthatthissetistocontaininitially *@parame5anotherelementthatthissetistocontaininitially *@throwsNullPointerExceptionifanyparametersarenull *@returnanenumsetinitiallycontainingthespecifiedelements */ publicstatic >EnumSet of(Ee1,Ee2,Ee3,Ee4, Ee5) { EnumSet result=noneOf(e1.getDeclaringClass()); result.add(e1); result.add(e2); result.add(e3); result.add(e4); result.add(e5); returnresult; } /** *n个元素枚举集合 * *@paramfirstanelementthatthesetistocontaininitially *@paramresttheremainingelementsthesetistocontaininitially *@throwsNullPointerExceptionifanyofthespecifiedelementsarenull, *orifrestisnull *@returnanenumsetinitiallycontainingthespecifiedelements */ @SafeVarargs publicstatic >EnumSet of(Efirst,E...rest){ EnumSet result=noneOf(first.getDeclaringClass()); result.add(first); for(Ee:rest) result.add(e); returnresult; } /** *区间内元素的枚举集合 * *@paramfromthefirstelementintherange *@paramtothelastelementintherange *@throwsNullPointerExceptionif{@codefrom}or{@codeto}arenull *@throwsIllegalArgumentExceptionif{@codefrom.compareTo(to)>0} *@returnanenumsetinitiallycontainingalloftheelementsinthe *rangedefinedbythetwospecifiedendpoints */ publicstatic >EnumSet range(Efrom,Eto){ if(from.compareTo(to)>0) thrownewIllegalArgumentException(from+">"+to); EnumSet result=noneOf(from.getDeclaringClass()); result.addRange(from,to); returnresult; } /** *Addsthespecifiedrangetothisenumset,whichisemptyprior *tothecall. */ abstractvoidaddRange(Efrom,Eto); /** *Returnsacopyofthisset. * *@returnacopyofthisset */ publicEnumSet clone(){ try{ return(EnumSet )super.clone(); }catch(CloneNotSupportedExceptione){ thrownewAssertionError(e); } } /** *Complementsthecontentsofthisenumset. */ abstractvoidcomplement(); /** *Throwsanexceptionifeisnotofthecorrecttypeforthisenumset. */ finalvoidtypeCheck(Ee){ ClasseClass=e.getClass(); if(eClass!=elementType&&eClass.getSuperclass()!=elementType) thrownewClassCastException(eClass+"!="+elementType); } /** *ReturnsallofthevaluescomprisingE. *Theresultisuncloned,cached,andsharedbyallcallers. */ privatestatic >E[]getUniverse(Class elementType){ returnSharedSecrets.getJavaLangAccess() .getEnumConstantsShared(elementType); } /** *ThisclassisusedtoserializeallEnumSetinstances,regardlessof *implementationtype.Itcapturestheir"logicalcontents"andthey *arereconstructedusingpublicstaticfactories.Thisisnecessary *toensurethattheexistenceofaparticularimplementationtypeis *animplementationdetail. * *@serialinclude */ privatestaticclassSerializationProxy > implementsjava.io.Serializable { /** *Theelementtypeofthisenumset. * *@serial */ privatefinalClass elementType; /** *Theelementscontainedinthisenumset. * *@serial */ privatefinalEnum[]elements; SerializationProxy(EnumSet set){ elementType=set.elementType; elements=set.toArray(ZERO_LENGTH_ENUM_ARRAY); } privateObjectreadResolve(){ EnumSet result=EnumSet.noneOf(elementType); for(Enume:elements) result.add((E)e); returnresult; } privatestaticfinallongserialVersionUID=362491234563181265L; } ObjectwriteReplace(){ returnnewSerializationProxy<>(this); } //readObjectmethodfortheserializationproxypattern //SeeEffectiveJava,SecondEd.,Item78. privatevoidreadObject(java.io.ObjectInputStreamstream) throwsjava.io.InvalidObjectException{ thrownewjava.io.InvalidObjectException("Proxyrequired"); } }
总结
以上就是本文关于阅读EnumSet抽象类源码的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站其他相关专题,如有不足之处,欢迎留言指出。感谢朋友们对本站的支持!
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。