OSDN Git Service

いろいろした
[chemicraft/chemicraft.git] / common / pcc / chemicraft / util / Formula.java
1 package pcc.chemicraft.util;
2
3 import java.util.ArrayList;
4
5 import pcc.chemicraft.ChemiCraftData;
6
7 /**
8  * 化学式から元素と数を取り出してArrayListに格納します。
9  * @author ponkotate
10  */
11 public class Formula {
12
13         private ArrayList<String> atoms = new ArrayList<String>();
14         private ArrayList<Integer> amonts = new ArrayList<Integer>();
15
16         public Formula(String par1) {
17                 this.run(par1);
18         }
19
20         public Formula(String[] par1, Integer[] par2) {
21                 this.setAtoms(par1, par2);
22         }
23
24         public Formula(ArrayList<String> par1, ArrayList<Integer> par2) {
25                 this.atoms = par1;
26                 this.amonts = par2;
27         }
28
29         public void run(String par1) {
30                 char[] var2 = par1.toCharArray();
31                 String var3 = null;
32                 int var4 = 0;
33                 int var5 = 1;
34                 Formula var6 = null;
35
36                 for (int i = 0; i < var2.length; i++) {
37                         if (Character.isLetter(var2[i])) {
38                                 if (Character.isUpperCase(var2[i])) {
39                                         if (var4 < 0) {
40                                                 var4 = 1;
41                                         }
42                                         if (var3 != null) {
43                                                 this.setAtom(var3,  var4 * var5);
44                                         } else if (var6 != null) {
45                                                 this.setAtoms(var6.getAtoms(), var6.getAmonts(), var4 * var5);
46                                                 var6 = null;
47                                         }
48                                         var3 = Character.toString(var2[i]);
49                                 } else {
50                                         var3 = var3.concat(Character.toString(var2[i]));
51                                 }
52                                 var4 = 0;
53                         } else if (Character.isDigit(var2[i])) {
54                                 if (var3 != null || var6 != null) {
55                                         var4 = var4 * 10 + Integer.valueOf(Character.toString(var2[i]));
56                                 } else {
57                                         var5 = Integer.valueOf(Character.toString(var2[i]));
58                                 }
59                         } else if (var2[i] == '・') {
60                                 if (var3 != null) {
61                                         this.setAtom(var3,  var4 * var5);
62                                 } else if (var6 != null) {
63                                         this.setAtoms(var6.getAtoms(), var6.getAmonts(), var4 * var5);
64                                 }
65                                 var3 = null;
66                                 var4 = 0;
67                                 var5 = 1;
68                                 var6 = null;
69                         } else if (var2[i] == '(') {
70                                 if (var3 != null) {
71                                         this.setAtom(var3,  var4 * var5);
72                                         var3 = null;
73                                         var4 = 1;
74                                 }
75                                 int var7 = par1.indexOf(')');
76                                 String var8 = par1.substring(i + 1, var7);
77                                 var6 = new Formula(var8);
78                                 i = var7;
79                         } else {
80                                 throw new IllegalArgumentException();
81                         }
82                 }
83                 if (var3 != null) {
84                         this.setAtom(var3,  var4 * var5);
85                 }
86         }
87
88         public boolean isAtom(String par1) {
89                 for (String var2:ChemiCraftData.ATOMSLIST) {
90                         if (par1.equalsIgnoreCase(var2)) {
91                                 return true;
92                         }
93                 }
94                 return false;
95         }
96
97         public void setAtom(String par1,  int par2) {
98                 if (isAtom(par1)) {
99                         int var3 = this.atoms.indexOf(par1);
100                         if (var3 == -1) {
101                                 this.atoms.add(par1);
102                                 this.amonts.add(par2);
103                         } else {
104                                 this.amonts.set(var3, this.amonts.get(var3) + par2);
105                         }
106                 } else {
107                         throw new IllegalArgumentException();
108                 }
109         }
110
111         public void setAtom(String par1,  char par2) {
112                 setAtom(par1, Integer.valueOf(Character.toString(par2)));
113         }
114
115         public void setAtoms(String[] par1, Integer[] par2) {
116                 setAtoms(par1, par2, 1);
117         }
118         public void setAtoms(String[] par1, Integer[] par2, int par3) {
119                 if (par1.length != par2.length) {
120                         throw new IndexOutOfBoundsException();
121                 }
122                 for (int i = 0; i < par1.length; i++) {
123                         this.setAtom(par1[i], par2[i] * par3);
124                 }
125         }
126
127         public String[] getAtoms() {
128                 return atoms.toArray(new String[atoms.size()]);
129         }
130
131         public Integer[] getAmonts() {
132                 return amonts.toArray(new Integer[amonts.size()]);
133         }
134
135 }