1)回调
jndi源码回调
public Object lookup(final String name) {
return execute(new JndiCallback<Object>() {
public Object doInContext(Context ctx) throws NamingException {
Object located = ctx.lookup(name);
return located;
} }); }
public <T> T execute(JndiCallback<T> contextCallback) throws NamingException {
Context ctx = getContext();
try {return contextCallback.doInContext(ctx);}
finally {releaseContext(ctx); }}
2) 枚举
public static enum ModelCategoryType{
V("V",1),AFA_P("AFA-P",2),AFA_N("AFA-N",3),U("U",4);
private final int priority;
private final String name;
ModelCategoryType(String name,int priority){
this.name=name;
this.priority=priority;
}
public int getPriority() {
return priority;
}
public String getName() {
return name;
}
public static int getPriorityFromName(String name){
if(name!=null){
for(ModelCategoryType modelCategoryType :ModelCategoryType.values()){
if(name.equals(modelCategoryType.getName())){
return modelCategoryType.getPriority();
}
}
}
return Integer.MAX_VALUE;
}
}
String newMdlCategory=RubyConstants.ModelCategoryType.V.getName();
RubyConstants.ModelCategoryType.getPriorityFromName(newMdlCategory)
<(RubyConstants.ModelCategoryType.getPriorityFromName(originalMdlCategory))
3) deep clone
/**
* This method makes a "deep clone" of any Java object it is given.
*/
public static Object deepClone(Object object) {
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(object);
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
ObjectInputStream ois = new ObjectInputStream(bais);
return ois.readObject();
}
catch (Exception e) {
e.printStackTrace();
return null;
}
}
// (1) create a Person object named Al
Address address = new Address("305 West Fern Avenue", "Palmer", "Alaska");
Person al = new Person("Al", "Alexander", address);
// (2) make a deep clone of Al
Person neighbor = (Person)deepClone(al);
转载于:https://blog.51cto.com/2828408/1841609