📄 raplatype.java
字号:
/*--------------------------------------------------------------------------*
| Copyright (C) 2006 Christopher Kohlhaas |
| |
| This program is free software; you can redistribute it and/or modify |
| it under the terms of the GNU General Public License as published by the |
| Free Software Foundation. A copy of the license has been included with |
| these distribution in the COPYING file, if not go to www.fsf.org |
| |
| As a special exception, you are granted the permissions to link this |
| program with every library, which license fulfills the Open Source |
| Definition as published by the Open Source Initiative (OSI). |
*--------------------------------------------------------------------------*/
package org.rapla.entities;
import java.io.Serializable;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import org.rapla.framework.RaplaException;
/**
* Enumeration Pattern for all Rapla objects. You should not instanciate Objects of this type,
* there is only one instance of RaplaType for each class of objects. You can get it via
* the object interface. E.g. Reservation.TYPE or Allocatable.TYPE
*/
public class RaplaType implements Serializable {
// Don't forget to increase the serialVersionUID when you change the fields
private static final long serialVersionUID = 1;
private String type;
private String localname;
private static Map registeredTypes = new HashMap();
public RaplaType(Class clazz, String localname) {
this( clazz.getName(), localname);
}
protected RaplaType(String name, String localname) {
this.type = name;
this.localname = localname;
if ( registeredTypes == null)
registeredTypes = new HashMap();
if ( registeredTypes.get( name ) != null) {
throw new IllegalStateException( "Type already registered");
}
registeredTypes.put( type, this);
}
static public RaplaType find( String typeName) throws RaplaException
{
try{
Class typeClass = Class.forName( typeName );
Field field = typeClass.getDeclaredField("TYPE");
RaplaType raplaType = (RaplaType) field.get( null );
return raplaType;
}
catch (Exception ex)
{
throw new RaplaException("Cant find Raplatype for name" + typeName, ex);
}
}
public boolean is(RaplaType other) {
if ( other == null)
return false;
return type.equals( other.type);
}
public String getLocalName() {
return localname;
}
public String toString() {
return type;
}
/** @deprecated Dangerous to use this method because a class registers itself on load time.
* So this method can return different results depending on how much RaplaTypes are currently loaded.*/
static public Iterator getRegisteredTypes() {
return registeredTypes.values().iterator();
}
public boolean equals( Object other) {
if ( !(other instanceof RaplaType))
return false;
return is( (RaplaType)other);
}
public int hashCode() {
return type.hashCode();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -