我目前正在开发一个核物理项目,我希望能够轻松访问所有核素。
技术规格如下:
Nuclides.get(Z,A,I)
或类似内容。 我尝试了这个,但它使用了反射方法来获取get方法(例如Nuclide.get("Li10")
),这似乎是一种过度杀伤力:
public class Nuclide {
public static String[] Symbols = {"H", "He", "Li", "Be", "B", "C", "N", "O", "F", "Ne", "Na", "Mg", "Al", "Si", "P", "S", "Cl", "Ar", "K", "Ca", "Sc", "Ti", "V", "Cr", "Mn", "Fe", "Co", "Ni", "Cu", "Zn", "Ga", "Ge", "As", "Se", "Br", "Kr", "Rb", "Sr", "Y", "Zr", "Nb", "Mo", "Tc", "Ru", "Rh", "Pd", "Ag", "Cd", "In", "Sn", "Sb", "Te", "I", "Xe", "Cs", "Ba", "La", "Ce", "Pr", "Nd", "Pm", "Sm", "Eu", "Gd", "Tb", "Dy", "Ho", "Er", "Tm", "Yb", "Lu", "Hf", "Ta", "W", "Re", "Os", "Ir", "Pt", "Au", "Hg", "Tl", "Pb", "Bi", "Po", "At", "Rn", "Fr", "Ra", "Ac", "Th", "Pa", "U", "Np", "Pu", "Am", "Cm", "Bk", "Cf", "Es", "Fm", "Md", "No", "Lr", "Rf", "Ha", "Sg", "Ns", "Hs", "Mt", "Ds","Rg"};
private String symbol;
private int Z;
private int A;
private int isomericState;
private String reactions;
private double decayTime;
private Nuclide() {}
private Nuclide(String symbol, int A, String isomericState, double decayTime, String reactions) {
this.symbol = symbol;
this.A = A;
this.isomericState = determineIsomericState(isomericState);
this.Z = determineZ(symbol);
}
private static int determineZ(String symbol) {
for (int i = 0; i < Symbols.length; i++) {
if (symbol.equals(Symbols[i])) return i + 1;
}
return -1;
}
private int determineIsomericState(String isomericState) {
if (isomericState.equals("gs")) this.isomericState = 0;
else if (isomericState.equals("m")) this.isomericState = 1;
else if (isomericState.equals("m2")) this.isomericState = 2;
else if (isomericState.equals("m3")) this.isomericState = 3;
else {
System.out.println(isomericState + " isomere inconu");
this.isomericState = -1;
}
return this.isomericState;
}
public static Nuclide get(String name) {
char[] nameArray = name.toCharArray();
String symbol = "";
String A = "";
String I = "";
boolean isSymbolRead = false, isARead = false;
for (char c : nameArray) {
if (Character.isLetter(c) && !isSymbolRead) {symbol += c;}
if (Character.isDigit(c) && !symbol.equals("") && !isARead) {
isSymbolRead = true;
A += c;
}
if (c == 'm' && isSymbolRead) {isARead = true;}
if (Character.isDigit(c) && isSymbolRead && isARead) {I += c;}
}
String classPath = Nuclide.class.getCanonicalName();
try {
Class<?> c = Class.forName(classPath + "$" + symbol);
Object[] objects = c.getEnumConstants();
for (Object obj : objects) {
if (obj.toString().equals(symbol + A + I)) {
Method method = obj.getClass().getDeclaredMethod("getNuclide");
Nuclide nuclide = (Nuclide) (method.invoke(obj));
return nuclide;
}
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
return null;
}
//Then all enums
public enum n {
n1(1, "gs", 6.146000e+02, "b-:1.000000e+02");
private Nuclide nuclide;
n(int A, String isomericState, double decayTime, String reactions) {this.nuclide = new Nuclide("n", A, isomericState, decayTime, reactions);}
public Nuclide getNuclide() {return nuclide;}
}
public enum H {
H1(1, "gs", 0.000000e+00, "s"),
H2(2, "gs", 0.000000e+00, "s"),
H3(3, "gs", 3.891050e+08, "b-:1.000000e+02"),
H4(4, "gs", 1.000000e-22, "n:1.000000e+02"),
H5(5, "gs", 8.000000e-23, "n:1.000000e+02"),
H6(6, "gs", 3.200000e-22, "nn:1.000000e+02");
private Nuclide nuclide;
H(int A, String isomericState, double decayTime, String reactions) {this.nuclide = new Nuclide("H", A, isomericState, decayTime, reactions);}
public Nuclide getNuclide() {return nuclide;}
}
public enum He {
He3(3, "gs", 0.000000e+00, "s"),
He4(4, "gs", 0.000000e+00, "s"),
He5(5, "gs", 7.600000e-22, "n:1.000000e+02"),
He6(6, "gs", 8.081000e-01, "b-:1.000000e+02"),
He7(7, "gs", 2.900000e-21, "n:1.000000e+02"),
He8(8, "gs", 1.220000e-01, "b-:8.800000e+01,b-n:1.200000e+01"),
He9(9, "gs", 7.000000e-21, "n:1.000000e+02"),
He10(10, "gs", 2.700000e-21, "nn:1.000000e+02");
private Nuclide nuclide;
He(int A, String isomericState, double decayTime, String reactions) {this.nuclide = new Nuclide("He", A, isomericState, decayTime, reactions);}
public Nuclide getNuclide() {return nuclide;}
}
public enum Li {
Li4(4, "gs", 9.100000e-23, "p:1.000000e+02"),
Li5(5, "gs", 3.000000e-22, "p:1.000000e+02"),
Li6(6, "gs", 0.000000e+00, "s"),
Li7(7, "gs", 0.000000e+00, "s"),
Li8(8, "gs", 8.380000e-01, "b-a:1.000000e+02"),
Li9(9, "gs", 1.783000e-01, "b-:5.050000e+01,b-n:4.950000e+01"),
Li10(10, "gs", 2.000000e-21, "n:1.000000e+02"),
Li11(11, "gs", 8.590000e-03, "b-:9.100000e+00,b-n:8.490000e+01,b-n:4.100000e+00,b-n:1.900000e+00"),
Li12(12, "gs", 1.000000e-08, "n:1.000000e+02");
private Nuclide nuclide;
Li(int A, String isomericState, double decayTime, String reactions) {this.nuclide = new Nuclide("Li", A, isomericState, decayTime, reactions);}
public Nuclide getNuclide() {return nuclide;}
}
// ...
}
我找到了另一篇仅适用于element table的帖子。我没有得到私人静态Holder课程的重点!
有没有更好的方法来定义Nuclide类(没有反射)或者我应该坚持这个定义?谢谢