View Javadoc

1   /**
2    * Copyright 2009 Timothy Johnston Licensed under the Apache License, Version 2.0 (the "License"); you may not use this
3    * file except in compliance with the License. You may obtain a copy of the License at
4    * 
5    * http://www.apache.org/licenses/LICENSE-2.0
6    * 
7    * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
8    * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
9    * specific language governing permissions and limitations under the License.
10   */
11  package com.timjohnstondev.unitconverter.logic;
12  
13  import javax.swing.JList;
14  import javax.swing.ListModel;
15  
16  /**
17   * Utility class that contains all of the logic for checking and updating the units with mole.
18   */
19  public final class UnitLogic
20  {
21    private UnitLogic()
22    {}
23  
24    /**
25     * Determines if there is a mole part of the unit already.
26     * 
27     * @param model list of the units
28     * @return true if unit has a mole part, otherwise false
29     */
30    public static boolean hasMoles(final ListModel model)
31    {
32      boolean hasMoles = false;
33      for (int i = 0; i < model.getSize(); i++)
34      {
35        final String currentUnit = (String) model.getElementAt(i);
36        if (currentUnit.indexOf("mol") > 0)
37        {
38          hasMoles = true;
39          break;
40        }
41      }
42      return hasMoles;
43    }
44  
45    /**
46     * Determines if the mole part of the unit will be in the divisor or not.
47     * 
48     * @param model list of the units
49     * @return true if mole unit is in the divisor, otherwise false
50     */
51    public static boolean isMolUnitDivisor(final ListModel model)
52    {
53      boolean isMolUnitDivisor = false;
54      String currentUnit = null;
55      for (int i = 0; i < model.getSize(); i++)
56      {
57        currentUnit = (String) model.getElementAt(i);
58        if (currentUnit.indexOf("/") > 0 && currentUnit.indexOf("kg") > currentUnit.indexOf("/"))
59        {
60          isMolUnitDivisor = true;
61          break;
62        }
63        else if (currentUnit.indexOf("kg") >= 0)
64        {
65          break;
66        }
67      }
68      return isMolUnitDivisor;
69    }
70  
71    private static void appendMolToUnit(final JList list, final String unitSeparator)
72    {
73      final ListModel model = list.getModel();
74      final String[] newData = new String[model.getSize()];
75      for (int i = 0; i < model.getSize(); i++)
76      {
77        final String currentUnit = (String) model.getElementAt(i);
78        String appendage = "";
79        if (currentUnit.indexOf("mol") < 0)
80        {
81          appendage = unitSeparator + "mol";
82        }
83        newData[i] = (String) model.getElementAt(i) + appendage;
84      }
85      list.setListData(newData);
86    }
87  
88    private static void appendMolToFirstDivisor(final JList list, final String unitSeparator)
89    {
90      final ListModel model = list.getModel();
91      final String[] newData = new String[model.getSize()];
92      for (int i = 0; i < model.getSize(); i++)
93      {
94        final StringBuffer bufferedUnit = new StringBuffer((String) model.getElementAt(i));
95        if (bufferedUnit.indexOf("mol") < 0)
96        {
97          final String appendage = unitSeparator + "mol";
98  
99          final int indexOfSlash = bufferedUnit.indexOf("/");
100         for (int indexOfSB = indexOfSlash + 1; indexOfSB < bufferedUnit.length(); indexOfSB++)
101         {
102           if (bufferedUnit.charAt(indexOfSB) < 'a' || bufferedUnit.charAt(indexOfSB) > 'z')
103           {
104             bufferedUnit.insert(indexOfSB, appendage);
105             break;
106           }
107         }
108       }
109       newData[i] = bufferedUnit.toString();
110     }
111     list.setListData(newData);
112   }
113 
114   private static void appendMolToLastDividend(final JList list, final String unitSeparator)
115   {
116     final ListModel model = list.getModel();
117     final String[] newData = new String[model.getSize()];
118     for (int i = 0; i < model.getSize(); i++)
119     {
120       final StringBuffer bufferedUnit = new StringBuffer((String) model.getElementAt(i));
121       if (bufferedUnit.indexOf("mol") < 0)
122       {
123         final String appendage = unitSeparator + "mol";
124 
125         final int indexOfSlash = bufferedUnit.indexOf("/");
126         bufferedUnit.insert(indexOfSlash, appendage);
127       }
128       newData[i] = bufferedUnit.toString();
129     }
130     list.setListData(newData);
131   }
132 
133   /**
134    * Changes the unit to show mole as appropriate.
135    * 
136    * @param list list of the units
137    * @param unitSeparator the unit separator
138    */
139   public static void insertMol(final JList list, final String unitSeparator)
140   {
141     final int selectedIndex = list.getSelectedIndex();
142     String currentUnit = null;
143     final ListModel model = list.getModel();
144     for (int i = 0; i < model.getSize(); i++)
145     {
146       currentUnit = (String) model.getElementAt(i);
147       if (currentUnit.endsWith("kg"))
148       {
149         appendMolToUnit(list, unitSeparator);
150         break;
151       }
152       else if (currentUnit.indexOf("/kg") >= 0)
153       {
154         appendMolToFirstDivisor(list, unitSeparator);
155         break;
156       }
157       else if (currentUnit.indexOf("kg/") >= 0)
158       {
159         appendMolToLastDividend(list, unitSeparator);
160         break;
161       }
162     }
163     list.setSelectedIndex(selectedIndex);
164   }
165 }