java enum 用法

/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2010, Red Hat Inc. or third-party contributors as
* indicated by the @author tags or express copyright attribution
* statements applied by the authors. All third-party contributions are
* distributed under license by Red Hat Inc.
*
* This copyrighted material is made available to anyone wishing to use, modify,
* copy, or redistribute it subject to the terms and conditions of the GNU
* Lesser General Public License, as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this distribution; if not, write to:
* Free Software Foundation, Inc.
* 51 Franklin Street, Fifth Floor
* Boston, MA 02110-1301 USA
*/
package org.hibernate.annotations;

import org.hibernate.cache.spi.access.AccessType;

/**
* Cache concurrency strategy.
*
* @author Emmanuel Bernard
*/
public enum CacheConcurrencyStrategy {
/**
* Indicates no concurrency strategy should be applied.
*/
NONE( null ),
/**
* Indicates that read-only strategy should be applied.
*
* @see AccessType#READ_ONLY
*/
READ_ONLY( AccessType.READ_ONLY ),
/**
* Indicates that the non-strict read-write strategy should be applied.
*
* @see AccessType#NONSTRICT_READ_WRITE
*/
NONSTRICT_READ_WRITE( AccessType.NONSTRICT_READ_WRITE ),
/**
* Indicates that the read-write strategy should be applied.
*
* @see AccessType#READ_WRITE
*/
READ_WRITE( AccessType.READ_WRITE ),
/**
* Indicates that the transaction strategy should be applied.
*
* @see AccessType#TRANSACTIONAL
*/
TRANSACTIONAL( AccessType.TRANSACTIONAL );

private final AccessType accessType;

private CacheConcurrencyStrategy(AccessType accessType) {
this.accessType = accessType;
}

/**
* Get the AccessType corresponding to this concurrency strategy.
*
* @return The corresponding concurrency strategy. Note that this will return {@code null} for
* {@link #NONE}
*/
public AccessType toAccessType() {
return accessType;
}

/**
* Conversion from {@link AccessType} to {@link CacheConcurrencyStrategy}.
*
* @param accessType The access type to convert
*
* @return The corresponding enum value. {@link #NONE} is returned by default if unable to
* recognize {@code accessType} or if {@code accessType} is {@code null}.
*/
public static CacheConcurrencyStrategy fromAccessType(AccessType accessType) {
if ( null == accessType ) {
return NONE;
}

switch ( accessType ) {
case READ_ONLY: {
return READ_ONLY;
}
case READ_WRITE: {
return READ_WRITE;
}
case NONSTRICT_READ_WRITE: {
return NONSTRICT_READ_WRITE;
}
case TRANSACTIONAL: {
return TRANSACTIONAL;
}
default: {
return NONE;
}
}
}

/**
* Parse an external representation of a CacheConcurrencyStrategy value.
*
* @param name The external representation
*
* @return The corresponding enum value, or {@code null} if not match was found.
*/
public static CacheConcurrencyStrategy parse(String name) {
if ( READ_ONLY.isMatch( name ) ) {
return READ_ONLY;
}
else if ( READ_WRITE.isMatch( name ) ) {
return READ_WRITE;
}
else if ( NONSTRICT_READ_WRITE.isMatch( name ) ) {
return NONSTRICT_READ_WRITE;
}
else if ( TRANSACTIONAL.isMatch( name ) ) {
return TRANSACTIONAL;
}
else if ( NONE.isMatch( name ) ) {
return NONE;
}
else {
return null;
}
}

private boolean isMatch(String name) {
return ( accessType != null && accessType.getExternalName().equalsIgnoreCase( name ) )
|| name().equalsIgnoreCase( name );
}
}

/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2008-2011, Red Hat Inc. or third-party contributors as
* indicated by the @author tags or express copyright attribution
* statements applied by the authors. All third-party contributions are
* distributed under license by Red Hat Inc.
*
* This copyrighted material is made available to anyone wishing to use, modify,
* copy, or redistribute it subject to the terms and conditions of the GNU
* Lesser General Public License, as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this distribution; if not, write to:
* Free Software Foundation, Inc.
* 51 Franklin Street, Fifth Floor
* Boston, MA 02110-1301 USA
*/
package org.hibernate.cache.spi.access;

/**
* The types of access strategies available.
*
* @author Steve Ebersole
*/
public enum AccessType {
/**
* Read-only access. Data may be added and removed, but not mutated.
*/
READ_ONLY( "read-only" ),
/**
* Read and write access (strict). Data may be added, removed and mutated.
*/
READ_WRITE( "read-write" ),
/**
* Read and write access (non-strict). Data may be added, removed and mutated. The non-strictness comes from
* the fact that locks are not maintained as tightly as in {@link #READ_WRITE}, which leads to better throughput
* but may also lead to inconsistencies.
*/
NONSTRICT_READ_WRITE( "nonstrict-read-write" ),
/**
* A read and write strategy where isolation/locking is maintained in conjunction with a JTA transaction.
*/
TRANSACTIONAL( "transactional" );

private final String externalName;

private AccessType(String externalName) {
this.externalName = externalName;
}

/**
* Get the corresponding externalized name for this value.
*
* @return The corresponding externalized name.
*/
public String getExternalName() {
return externalName;
}

@Override
public String toString() {
return "AccessType[" + externalName + "]";
}

/**
* Resolve an AccessType from its external name.
*
* @param externalName The external representation to resolve
*
* @return The access type.
*
* @throws UnknownAccessTypeException If the externalName was not recognized.
*
* @see #getExternalName()
*/
public static AccessType fromExternalName(String externalName) {
if ( externalName == null ) {
return null;
}
for ( AccessType accessType : AccessType.values() ) {
if ( accessType.getExternalName().equals( externalName ) ) {
return accessType;
}
}
throw new UnknownAccessTypeException( externalName );
}
}

时间: 2024-10-01 05:19:20

java enum 用法的相关文章

Java Enum用法详解

Java Enum用法详解 用法一:常量 在JDK1.5 之前,我们定义常量都是: public static fianl.... .现在好了,有了枚举,可以把相关的常量分组到一个枚举类型里,而且枚举提供了比常量更多的方法. public enum Color { RED, GREEN, BLANK, YELLOW } 用法二:switch JDK1.6之前的switch语句只支持int,char,enum类型,使用枚举,能让我们的代码可读性更强. enum Signal { GREEN, YE

java Enum 用法示例

public enum MyEnum { Monday, Tuesday, Wednesday, Thursady, Friday, Saturday, Sunday; public static void main(String[]args){ //Enum 对象 MyEnum mye; mye=MyEnum.Sunday; MyEnum mye1=MyEnum.Monday; /** * enum convert to int * int java.lang.Enum.ordinal() *

Java Enum解析【转】

Enum用法: 1:常量 在JDK1.5 之前,我们定义常量都是: public static fianl.... .现在好了,有了枚举,可以把相关的常量分组到一个枚举类型里,而且枚举提供了比常量更多的方法. public enum Color { RED, GREEN, BLANK, YELLOW } 2:switch JDK1.6之前的switch语句只支持int,char,enum类型,使用枚举,能让我们的代码可读性更强. enum Signal { GREEN, YELLOW, RED

Rhythmk 一步一步学 JAVA (20) JAVA enum常用方法

JAVA 枚举定义常用方法: 1.static Enum valueOf(Class enum,String name) 返回指定name的枚举类型 2.Static Enum values[] 返回枚举常量集合 ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50

java enum

小谈Java Enum的多态性 博客分类: Java JavaAppleJDKJVMIDEA Enum+多态,我没说错,不过Enum是不可以被继承的,也不可以继承自别人,只是能实现接口而已,何谈多态?不过还是先看看"现象"吧: Java代码   public enum Fruit { APPLE, PEAR, PEACH, ORANGE; } 以上是一个简单的enum,关于它,我要补充一点: Fruit是java.lang.Enum的子类,准确地说,是Enum<Fruit>

Java RMI 用法总结

RMI就是远程方法调用的简写.顾名思义,就是让一台机器上的对象调用另外一个机器上的对象.RMI的用法非常简单,首先是服务端定义一个接口(接口要扩展Remote接口),再实现这个接口(要扩展UnicastRemoteObject),再绑定到Naming静态类中.客户端通过Naming获取一个远程对象,就可以像普通的对象一样调用远程对象了.RMI中有个Stub类,它的作用就是代理服务器的接口对象,负责将方法的调用转换成网络请求发送给服务器,再从服务器返回对象进行解码.在JDK1.5中,Stub类会自

C/C++ enum 用法

enum box{pencil,pen};//这里你就定义了一个枚举类型的变量叫box,这个枚举变量内含有两个元素也称枚举元素在这里是pencil和pen,分别表示铅笔和钢笔. ? 1 enum {pencil,pen}box,box2; //在声明的同时进行定义! void main(void) { enum egg {a,b,c}; enum egg test; //在这里你可以简写成egg test; test = c; //对枚举变量test进行赋予元素操作,这里之所以叫赋元素操作不叫赋

最全面的Java多线程用法解析

最全面的java多线程用法解析,如果你对Java的多线程机制并没有深入的研究,那么本文可以帮助你更透彻地理解Java多线程的原理以及使用方法. 1.创建线程 在Java中创建线程有两种方法:使用Thread类和使用Runnable接口.在使用Runnable接口时需要建立一个Thread实例.因此,无论是通过Thread类还是Runnable接口建立线程,都必须建立Thread类或它的子类的实例.Thread构造函数: public Thread( ); public Thread(Runnab

Java enum枚举是怎么回事

Java1.5之前是没有枚举的,如果想使用类似枚举的特性,也即是需要使用常量的时候,可以通过如下代码完成: Java中的常量定义: publicclassSex2 { publicstaticfinalintGIRL = 1; publicstaticfinalintBOY = 2; publicstaticvoid main(String[]args) { System.out.println(Sex2.GIRL); } } 但是这样做有个缺点,就是不够直观,我们打印出Sex2.GIRL,看到