OSDN Git Service

e145ecf363866a9570feaef5c81941297e481921
[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 public class Formula
8 {
9
10         private ArrayList<String> atoms = new ArrayList<String>();
11         private ArrayList<Integer> amonts = new ArrayList<Integer>();
12
13         public Formula(String par1)
14         {
15                 this.run(par1);
16         }
17
18         public Formula(String[] par1, Integer[] par2)
19         {
20                 this.setAtoms(par1, par2);
21         }
22
23         public Formula(ArrayList<String> par1, ArrayList<Integer> par2)
24         {
25                 this.atoms = par1;
26                 this.amonts = par2;
27         }
28
29         public void run(String par1)
30         {
31                 char[] var2 = par1.toCharArray();
32                 String var3 = null;
33                 int var4 = 0;
34                 int var5 = 1;
35                 Formula var6 = null;
36
37                 for (int i = 0; i < var2.length; i++)
38                 {
39                         if (Character.isLetter(var2[i])){
40                                 if (Character.isUpperCase(var2[i])){
41                                         if (var4 < 0){
42                                                 var4 = 1;
43                                         }
44                                         if (var3 != null)
45                                         {
46                                                 this.setAtom(var3,  var4 * var5);
47                                         } else if (var6 != null) {
48                                                 this.setAtoms(var6.getAtoms(), var6.getAmonts(), var4 * var5);
49                                                 var6 = null;
50                                         }
51                                         var3 = Character.toString(var2[i]);
52                                 } else {
53                                         var3 = var3.concat(Character.toString(var2[i]));
54                                 }
55                                 var4 = 0;
56                         } else if (Character.isDigit(var2[i])) {
57                                 if (var3 != null || var6 != null) {
58                                         var4 = var4 * 10 + Integer.valueOf(Character.toString(var2[i]));
59                                 } else {
60                                         var5 = Integer.valueOf(Character.toString(var2[i]));
61                                 }
62                         } else if (var2[i] == '・') {
63                                 if (var3 != null)
64                                 {
65                                         this.setAtom(var3,  var4 * var5);
66                                 } else if (var6 != null) {
67                                         this.setAtoms(var6.getAtoms(), var6.getAmonts(), var4 * var5);
68                                 }
69                                 var3 = null;
70                                 var4 = 0;
71                                 var5 = 1;
72                                 var6 = null;
73                         } else if (var2[i] == '(') {
74                                 if (var3 != null)
75                                 {
76                                         this.setAtom(var3,  var4 * var5);
77                                         var3 = null;
78                                         var4 = 1;
79                                 }
80                                 int var7 = par1.indexOf(')');
81                                 String var8 = par1.substring(i + 1, var7);
82                                 var6 = new Formula(var8);
83                                 i = var7;
84                         } else {
85                                 throw new IllegalArgumentException();
86                         }
87                 }
88                 if (var3 != null)
89                 {
90                         this.setAtom(var3,  var4 * var5);
91                 }
92         }
93
94         public boolean isAtom(String par1)
95         {
96                 for (String var2:ChemiCraftData.ATOMSLIST){
97                         if (par1.equalsIgnoreCase(var2)){
98                                 return true;
99                         }
100                 }
101                 return false;
102         }
103
104         public void setAtom(String par1,  int par2)
105         {
106                 if (isAtom(par1)){
107                         int var3 = this.atoms.indexOf(par1);
108                         if (var3 == -1){
109                                 this.atoms.add(par1);
110                                 this.amonts.add(par2);
111                         } else {
112                                 this.amonts.set(var3, this.amonts.get(var3) + par2);
113                         }
114                 } else {
115                         throw new IllegalArgumentException();
116                 }
117         }
118
119         public void setAtom(String par1,  char par2)
120         {
121                 setAtom(par1, Integer.valueOf(Character.toString(par2)));
122         }
123
124         public void setAtoms(String[] par1, Integer[] par2)
125         {
126                 setAtoms(par1, par2, 1);
127         }
128         public void setAtoms(String[] par1, Integer[] par2, int par3)
129         {
130                 if (par1.length != par2.length)
131                 {
132                         throw new IndexOutOfBoundsException();
133                 }
134                 for (int i = 0; i < par1.length; i++)
135                 {
136                         this.setAtom(par1[i], par2[i] * par3);
137                 }
138         }
139
140         public String[] getAtoms()
141         {
142                 return atoms.toArray(new String[atoms.size()]);
143         }
144
145         public Integer[] getAmonts()
146         {
147                 return amonts.toArray(new Integer[amonts.size()]);
148         }
149
150 }