Coverage Report - com.timjohnstondev.unitconverter.logic.UnitLogic
 
Classes in this File Line Coverage Branch Coverage Complexity
UnitLogic
98%
68/69
89%
34/38
3.714
 
 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  0
   {}
 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  2
     boolean hasMoles = false;
 33  5
     for (int i = 0; i < model.getSize(); i++)
 34  
     {
 35  4
       final String currentUnit = (String) model.getElementAt(i);
 36  4
       if (currentUnit.indexOf("mol") > 0)
 37  
       {
 38  1
         hasMoles = true;
 39  1
         break;
 40  
       }
 41  
     }
 42  2
     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  4
     boolean isMolUnitDivisor = false;
 54  4
     String currentUnit = null;
 55  11
     for (int i = 0; i < model.getSize(); i++)
 56  
     {
 57  10
       currentUnit = (String) model.getElementAt(i);
 58  10
       if (currentUnit.indexOf("/") > 0 && currentUnit.indexOf("kg") > currentUnit.indexOf("/"))
 59  
       {
 60  1
         isMolUnitDivisor = true;
 61  1
         break;
 62  
       }
 63  9
       else if (currentUnit.indexOf("kg") >= 0)
 64  
       {
 65  2
         break;
 66  
       }
 67  
     }
 68  4
     return isMolUnitDivisor;
 69  
   }
 70  
 
 71  
   private static void appendMolToUnit(final JList list, final String unitSeparator)
 72  
   {
 73  2
     final ListModel model = list.getModel();
 74  2
     final String[] newData = new String[model.getSize()];
 75  8
     for (int i = 0; i < model.getSize(); i++)
 76  
     {
 77  6
       final String currentUnit = (String) model.getElementAt(i);
 78  6
       String appendage = "";
 79  6
       if (currentUnit.indexOf("mol") < 0)
 80  
       {
 81  6
         appendage = unitSeparator + "mol";
 82  
       }
 83  6
       newData[i] = (String) model.getElementAt(i) + appendage;
 84  
     }
 85  2
     list.setListData(newData);
 86  2
   }
 87  
 
 88  
   private static void appendMolToFirstDivisor(final JList list, final String unitSeparator)
 89  
   {
 90  2
     final ListModel model = list.getModel();
 91  2
     final String[] newData = new String[model.getSize()];
 92  8
     for (int i = 0; i < model.getSize(); i++)
 93  
     {
 94  6
       final StringBuffer bufferedUnit = new StringBuffer((String) model.getElementAt(i));
 95  6
       if (bufferedUnit.indexOf("mol") < 0)
 96  
       {
 97  6
         final String appendage = unitSeparator + "mol";
 98  
 
 99  6
         final int indexOfSlash = bufferedUnit.indexOf("/");
 100  16
         for (int indexOfSB = indexOfSlash + 1; indexOfSB < bufferedUnit.length(); indexOfSB++)
 101  
         {
 102  16
           if (bufferedUnit.charAt(indexOfSB) < 'a' || bufferedUnit.charAt(indexOfSB) > 'z')
 103  
           {
 104  6
             bufferedUnit.insert(indexOfSB, appendage);
 105  6
             break;
 106  
           }
 107  
         }
 108  
       }
 109  6
       newData[i] = bufferedUnit.toString();
 110  
     }
 111  2
     list.setListData(newData);
 112  2
   }
 113  
 
 114  
   private static void appendMolToLastDividend(final JList list, final String unitSeparator)
 115  
   {
 116  1
     final ListModel model = list.getModel();
 117  1
     final String[] newData = new String[model.getSize()];
 118  4
     for (int i = 0; i < model.getSize(); i++)
 119  
     {
 120  3
       final StringBuffer bufferedUnit = new StringBuffer((String) model.getElementAt(i));
 121  3
       if (bufferedUnit.indexOf("mol") < 0)
 122  
       {
 123  3
         final String appendage = unitSeparator + "mol";
 124  
 
 125  3
         final int indexOfSlash = bufferedUnit.indexOf("/");
 126  3
         bufferedUnit.insert(indexOfSlash, appendage);
 127  
       }
 128  3
       newData[i] = bufferedUnit.toString();
 129  
     }
 130  1
     list.setListData(newData);
 131  1
   }
 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  6
     final int selectedIndex = list.getSelectedIndex();
 142  6
     String currentUnit = null;
 143  6
     final ListModel model = list.getModel();
 144  13
     for (int i = 0; i < model.getSize(); i++)
 145  
     {
 146  12
       currentUnit = (String) model.getElementAt(i);
 147  12
       if (currentUnit.endsWith("kg"))
 148  
       {
 149  2
         appendMolToUnit(list, unitSeparator);
 150  2
         break;
 151  
       }
 152  10
       else if (currentUnit.indexOf("/kg") >= 0)
 153  
       {
 154  2
         appendMolToFirstDivisor(list, unitSeparator);
 155  2
         break;
 156  
       }
 157  8
       else if (currentUnit.indexOf("kg/") >= 0)
 158  
       {
 159  1
         appendMolToLastDividend(list, unitSeparator);
 160  1
         break;
 161  
       }
 162  
     }
 163  6
     list.setSelectedIndex(selectedIndex);
 164  6
   }
 165  
 }