Coverage Report - de.jollyday.CalendarHierarchy
 
Classes in this File Line Coverage Branch Coverage Complexity
CalendarHierarchy
75%
18/24
62%
5/8
1,6
 
 1  
 /**
 2  
  * Copyright 2010 Sven Diedrichsen 
 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 
 12  
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either 
 13  
  * express or implied. See the License for the specific language 
 14  
  * governing permissions and limitations under the License. 
 15  
  */
 16  
 package de.jollyday;
 17  
 
 18  
 import java.util.HashMap;
 19  
 import java.util.Locale;
 20  
 import java.util.Map;
 21  
 
 22  
 import de.jollyday.util.ResourceUtil;
 23  
 
 24  
 /**
 25  
  * Bean class for describing the configuration hierarchy.
 26  
  * 
 27  
  * @author Sven Diedrichsen
 28  
  * @version $Id: $
 29  
  */
 30  
 public class CalendarHierarchy {
 31  
         private String id;
 32  909
         private Map<String, CalendarHierarchy> children = new HashMap<>();
 33  
         private final CalendarHierarchy parent;
 34  909
         private ResourceUtil resourceUtil = new ResourceUtil();
 35  
         private String fallbackDescription;
 36  
 
 37  
         /**
 38  
          * Constructor which takes a eventually existing parent hierarchy node and
 39  
          * the ID of this hierarchy.
 40  
          * 
 41  
          * @param parent
 42  
          *            a {@link de.jollyday.CalendarHierarchy} object.
 43  
          * @param id
 44  
          *            a {@link java.lang.String} object.
 45  
          */
 46  909
         public CalendarHierarchy(CalendarHierarchy parent, String id) {
 47  909
                 this.parent = parent;
 48  909
                 this.id = id;
 49  909
         }
 50  
 
 51  
         /**
 52  
          * <p>
 53  
          * Getter for the field <code>id</code>.
 54  
          * </p>
 55  
          * 
 56  
          * @return the id
 57  
          */
 58  
         public String getId() {
 59  3022
                 return id;
 60  
         }
 61  
 
 62  
         /**
 63  
          * Returns the country description for the default locale.
 64  
          * 
 65  
          * @return the description
 66  
          */
 67  
         public String getDescription() {
 68  673
                 return getDescription(Locale.getDefault());
 69  
         }
 70  
 
 71  
         /**
 72  
          * Returns the hierarchies description text from the resource bundle.
 73  
          * 
 74  
          * @param l
 75  
          *            Locale to return the description text for.
 76  
          * @return Description text
 77  
          */
 78  
         public String getDescription(Locale l) {
 79  673
                 String descr = resourceUtil.getCountryDescription(l, getPropertiesKey());
 80  673
                 if (ResourceUtil.UNDEFINED.equals(descr) && fallbackDescription != null) {
 81  276
                         descr = fallbackDescription;
 82  
                 }
 83  673
                 return descr;
 84  
         }
 85  
 
 86  
         /**
 87  
          * Recursively returns the properties key to retrieve the description from
 88  
          * the localized resource bundle.
 89  
          * 
 90  
          * @return
 91  
          */
 92  
         private String getPropertiesKey() {
 93  1305
                 if (parent != null) {
 94  632
                         return parent.getPropertiesKey() + "." + getId();
 95  
                 }
 96  673
                 return getId();
 97  
         }
 98  
 
 99  
         /**
 100  
          * {@inheritDoc}
 101  
          * 
 102  
          * Compares Hierarchies by id.
 103  
          */
 104  
         @Override
 105  
         public boolean equals(Object obj) {
 106  0
                 if (obj instanceof CalendarHierarchy) {
 107  0
                         return ((CalendarHierarchy) obj).getId().equals(this.getId());
 108  
                 }
 109  0
                 return super.equals(obj);
 110  
         }
 111  
 
 112  
         @Override
 113  
         public int hashCode() {
 114  0
                 return getId().hashCode();
 115  
         }
 116  
 
 117  
         /**
 118  
          * <p>
 119  
          * Setter for the field <code>children</code>.
 120  
          * </p>
 121  
          * 
 122  
          * @param children
 123  
          *            the children to set
 124  
          */
 125  
         public void setChildren(final Map<String, CalendarHierarchy> children) {
 126  0
                 this.children = children;
 127  0
         }
 128  
 
 129  
         /**
 130  
          * <p>
 131  
          * Getter for the field <code>children</code>.
 132  
          * </p>
 133  
          * 
 134  
          * @return the children
 135  
          */
 136  
         public Map<String, CalendarHierarchy> getChildren() {
 137  2270
                 return children;
 138  
         }
 139  
 
 140  
         /**
 141  
          * @param description the fallback description
 142  
          */
 143  
         public void setFallbackDescription(String description) {
 144  909
                 this.fallbackDescription = description;
 145  909
         }
 146  
 
 147  
 }