Coverage Report - net.sourceforge.jeuclid.elements.support.operatordict.AbstractOperatorDictionary
 
Classes in this File Line Coverage Branch Coverage Complexity
AbstractOperatorDictionary
51%
24/47
55%
11/20
3,5
 
 1  
 /*
 2  
  * Copyright 2002 - 2008 JEuclid, http://jeuclid.sf.net
 3  
  * 
 4  
  * Licensed under the Apache License, Version 2.0 (the "License");
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at
 7  
  *
 8  
  *      http://www.apache.org/licenses/LICENSE-2.0
 9  
  *
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13  
  * See the License for the specific language governing permissions and
 14  
  * limitations under the License.
 15  
  */
 16  
 
 17  
 /* $Id: AbstractOperatorDictionary.java,v c75d8b379394 2009/09/25 20:03:08 max $ */
 18  
 
 19  
 package net.sourceforge.jeuclid.elements.support.operatordict;
 20  
 
 21  
 import java.io.IOException;
 22  
 import java.io.InputStream;
 23  
 import java.io.ObjectInput;
 24  
 import java.io.ObjectInputStream;
 25  
 import java.io.Serializable;
 26  
 import java.util.EnumMap;
 27  
 import java.util.Map;
 28  
 
 29  
 /**
 30  
  * Read default values of operators from xml file.
 31  
  * 
 32  
  * @version $Revision: c75d8b379394 $
 33  
  */
 34  
 public abstract class AbstractOperatorDictionary implements OperatorDictionary,
 35  
         Serializable {
 36  
 
 37  
     /**
 38  
      * 
 39  
      */
 40  
     private static final long serialVersionUID = 1L;
 41  
 
 42  
     private final Map<OperatorAttribute, Map<String, Map<OperatorForm, String>>> dict;
 43  
 
 44  
     /**
 45  
      * Default constructor.
 46  
      */
 47  0
     protected AbstractOperatorDictionary() {
 48  0
         this.dict = new EnumMap<OperatorAttribute, Map<String, Map<OperatorForm, String>>>(
 49  
                 OperatorAttribute.class);
 50  0
         this.initializeFromXML(this.dict);
 51  0
         this.overrideStretchy();
 52  0
     }
 53  
 
 54  
     private void overrideStretchy() {
 55  0
         final Map<String, Map<OperatorForm, String>> opmap = this.dict
 56  
                 .get(OperatorAttribute.STRETCHY);
 57  0
         for (final Map.Entry<String, Map<OperatorForm, String>> e : opmap
 58  
                 .entrySet()) {
 59  0
             final String override = StretchOverride.getStretchOverride(e
 60  
                     .getKey());
 61  0
             if (override != null) {
 62  0
                 for (final Map.Entry<OperatorForm, String> e2 : e.getValue()
 63  
                         .entrySet()) {
 64  0
                     if (Boolean.parseBoolean(e2.getValue())) {
 65  0
                         e2.setValue(override);
 66  
                     }
 67  
                 }
 68  
             }
 69  0
         }
 70  0
     }
 71  
 
 72  
     /**
 73  
      * Get the for singleton instance.
 74  
      * 
 75  
      * @param path
 76  
      *            path for the serialized object.
 77  
      * @return an instance of OperatorDictionary.
 78  
      */
 79  
     protected static OperatorDictionary deserialize(final String path) {
 80  418
         OperatorDictionary newDict = null;
 81  
         try {
 82  418
             final InputStream is = AbstractOperatorDictionary.class
 83  
                     .getResourceAsStream(path);
 84  418
             final ObjectInput oi = new ObjectInputStream(is);
 85  418
             newDict = (AbstractOperatorDictionary) oi.readObject();
 86  418
             oi.close();
 87  0
         } catch (final ClassNotFoundException cnfe) {
 88  0
             newDict = null;
 89  0
         } catch (final IllegalArgumentException e) {
 90  0
             newDict = null;
 91  0
         } catch (final IOException e) {
 92  0
             newDict = null;
 93  0
         } catch (final NullPointerException e) {
 94  0
             newDict = null;
 95  418
         }
 96  418
         return newDict;
 97  
     }
 98  
 
 99  
     /**
 100  
      * Initializes Dictionary.
 101  
      * 
 102  
      * @param d
 103  
      *            the dictionary to initialize.
 104  
      */
 105  
     protected abstract void initializeFromXML(
 106  
             Map<OperatorAttribute, Map<String, Map<OperatorForm, String>>> d);
 107  
 
 108  
     /**
 109  
      * Determines default value of the operator attribute.
 110  
      * 
 111  
      * @param operator
 112  
      *            operator character
 113  
      * @param form
 114  
      *            form string
 115  
      * @param attributeName
 116  
      *            name of attribute
 117  
      * @return VALUE_UNKOWN or value from dict.
 118  
      * @throws UnknownAttributeException
 119  
      *             Raised, if wrong attributeName was provided.
 120  
      */
 121  
     public String getDefaultAttributeValue(final String operator,
 122  
             final String form, final String attributeName)
 123  
             throws UnknownAttributeException {
 124  21823572
         final OperatorForm intForm = OperatorForm.parseOperatorForm(form);
 125  21823572
         return this.getDefaultAttributeValue(operator, intForm,
 126  
                 OperatorAttribute.parseOperatorAttribute(attributeName));
 127  
     }
 128  
 
 129  
     /**
 130  
      * Determines default value of the operator attribute.
 131  
      * 
 132  
      * @param operator
 133  
      *            Operator character.
 134  
      * @param form
 135  
      *            Form value
 136  
      * @param attributeName
 137  
      *            Name of the attribute.
 138  
      * @return Default value (VALUE_UNKNOWN, if default value has not been
 139  
      *         provided yet).
 140  
      * @throws UnknownAttributeException
 141  
      *             Raised, if wrong attributeName was provided.
 142  
      */
 143  
     private String getDefaultAttributeValue(final String operator,
 144  
             final OperatorForm form, final OperatorAttribute attribute) {
 145  
 
 146  21823154
         final Map<String, Map<OperatorForm, String>> opForAttr = this.dict
 147  
                 .get(attribute);
 148  21823154
         if (opForAttr == null) {
 149  3117056
             return attribute.getDefaultValue();
 150  
         }
 151  18706098
         final Map<OperatorForm, String> valuesPerForm = opForAttr.get(operator);
 152  
         String retVal;
 153  18706098
         if (valuesPerForm == null) {
 154  8560308
             retVal = attribute.getDefaultValue();
 155  
         } else {
 156  10145790
             retVal = valuesPerForm.get(form);
 157  10145790
             if (retVal == null) {
 158  9178700
                 retVal = valuesPerForm.get(OperatorForm.INFIX);
 159  
             }
 160  10145790
             if (retVal == null) {
 161  9145422
                 retVal = valuesPerForm.get(OperatorForm.POSTFIX);
 162  
             }
 163  10145790
             if (retVal == null) {
 164  8129473
                 retVal = valuesPerForm.get(OperatorForm.PREFIX);
 165  
             }
 166  10145790
             if (retVal == null) {
 167  0
                 retVal = attribute.getDefaultValue();
 168  
             }
 169  
         }
 170  18706098
         return retVal;
 171  
     }
 172  
 
 173  
 }