자바 매 거 진 형식 에 새로운 방법 추가
5754 단어 SDN 연구
public enum Color {
RED(" ", 1), GREEN(" ", 2), BLANK(" ", 3), YELLO(" ", 4);
//
private String name;
private int index;
//
private Color(String name, int index) {
this.name = name;
this.index = index;
}
public static String getName(int index) {
// values() ;
for (Color c : Color.values()) {
if (c.getIndex() == index) {
return c.name;
}
}
return null;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
중요 한 것 은 해당 하 는 매 거 진 구성원 의 방법 을 사용 하여 해당 하 는 대상 을 만 들 수 있다 는 것 이다.예 를 들 어 아래 의 OFtype 은 이렇게 사용 할 수 있다.
OFType t = OFType.HELLO;
t.newInstance();
다음은 Floodlight controller 의 지식 포인트 구현 입 니 다.
public enum OFType {
// ,
HELLO (0, OFHello.class, new Instantiable() {
@Override
public OFMessage instantiate() {
return new OFHello();
}}),
ERROR (1, OFError.class, new Instantiable() {
@Override
public OFMessage instantiate() {
return new OFError();
}}),
PACKET_IN (10, OFPacketIn.class, new Instantiable() {
@Override
public OFMessage instantiate() {
return new OFPacketIn();
}}),
PACKET_OUT (13, OFPacketOut.class, new Instantiable() {
@Override
public OFMessage instantiate() {
return new OFPacketOut();
}}),
FLOW_MOD (14, OFFlowMod.class, new Instantiable() {
@Override
public OFMessage instantiate() {
return new OFFlowMod();
}});
static OFType[] mapping;
// ,
protected Class extends OFMessage> clazz;
//
protected Constructor extends OFMessage> constructor;
// Instantiable , OFMessage
protected Instantiable instantiable;
//
protected byte type;
/**
* Store some information about the OpenFlow type, including wire protocol
* type number, length, and derived class
*
* @param type Wire protocol number associated with this OFType
* @param requestClass The Java class corresponding to this type of OpenFlow message
* @param instantiator An Instantiator implementation that creates an
* instance of the specified OFMessage
*/
OFType(int type, Class extends OFMessage> clazz, Instantiable instantiator) {
this.type = (byte) type;
this.clazz = clazz;
this.instantiable = instantiator;
try {
this.constructor = clazz.getConstructor(new Class[]{});
} catch (Exception e) {
throw new RuntimeException("Failure getting constructor for class: " + clazz, e);
}
OFType.addMapping(this.type, this); //
}
/**
* Adds a mapping from type value to OFType enum
*
* @param i OpenFlow wire protocol type
* @param t type
*/
static public void addMapping(byte i, OFType t) {
if (mapping == null)
mapping = new OFType[32];
OFType.mapping[i] = t;
}
/**
* Remove a mapping from type value to OFType enum
*
* @param i OpenFlow wire protocol type
*/
static public void removeMapping(byte i) {
OFType.mapping[i] = null;
}
/**
* Given a wire protocol OpenFlow type number, return the OFType associated
* with it
*
* @param i wire protocol number
* @return OFType enum type
*/
static public OFType valueOf(Byte i) {
return OFType.mapping[i];
}
/**
* @return Returns the wire protocol value corresponding to this OFType
*/
public byte getTypeValue() {
return this.type;
}
/**
* @return return the OFMessage subclass corresponding to this OFType
*/
public Class extends OFMessage> toClass() {
return clazz;
}
/**
* Returns the no-argument Constructor of the implementation class for
* this OFType
* @return the constructor
*/
public Constructor extends OFMessage> getConstructor() {
return constructor;
}
/**
* Returns a new instance of the OFMessage represented by this OFType
* @return the new object
*/
public OFMessage newInstance() {
return instantiable.instantiate();
}
/**
* @return the instantiable
*/
public Instantiable getInstantiable() {
return instantiable;
}
/**
* @param instantiable the instantiable to set
*/
public void setInstantiable(Instantiable instantiable) {
this.instantiable = instantiable;
}
}
: