Coverage Report - de.smartics.properties.api.core.context.alias.PropertyAliasMapping
 
Classes in this File Line Coverage Branch Coverage Complexity
PropertyAliasMapping
0%
0/24
0%
0/8
2.167
 
 1  
 /*
 2  
  * Copyright 2012-2013 smartics, Kronseder & Reiner GmbH
 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  
 package de.smartics.properties.api.core.context.alias;
 17  
 
 18  
 import java.io.Serializable;
 19  
 import java.util.HashMap;
 20  
 import java.util.Map;
 21  
 import java.util.Map.Entry;
 22  
 
 23  
 import javax.annotation.concurrent.NotThreadSafe;
 24  
 
 25  
 import de.smartics.util.lang.Arg;
 26  
 import de.smartics.util.lang.BlankArgumentException;
 27  
 
 28  
 /**
 29  
  * The mapping of alias names of property reports to their physical names. The
 30  
  * physical names are those file names that are stored in the
 31  
  * <code>META-INF</code> folder or on the server in the case of HTML reports.
 32  
  * The alias names are alternative, unique naming schemes that refer to the same
 33  
  * files.
 34  
  */
 35  
 @NotThreadSafe
 36  
 public final class PropertyAliasMapping implements Serializable
 37  
 {
 38  
   // ********************************* Fields *********************************
 39  
 
 40  
   // --- constants ------------------------------------------------------------
 41  
 
 42  
   /**
 43  
    * The class version identifier.
 44  
    */
 45  
   private static final long serialVersionUID = 1L;
 46  
 
 47  
   // --- members --------------------------------------------------------------
 48  
 
 49  
   /**
 50  
    * The mapping of alias names of property reports to their physical names. The
 51  
    * physical names are those file names that are stored in the
 52  
    * <code>META-INF</code> folder or on the server in the case of HTML reports.
 53  
    * The alias names are alternative, unique naming schemes that refer to the
 54  
    * same files.
 55  
    * <p>
 56  
    * The key is the name of the alias, the value is the physical resource. There
 57  
    * is only one physical for each alias, but different aliases may point to the
 58  
    * same resource.
 59  
    * </p>
 60  
    *
 61  
    * @serial
 62  
    */
 63  0
   private final Map<String, String> aliasMapping =
 64  
       new HashMap<String, String>();
 65  
 
 66  
   // ****************************** Initializer *******************************
 67  
 
 68  
   // ****************************** Constructors ******************************
 69  
 
 70  
   /**
 71  
    * Default constructor.
 72  
    */
 73  
   public PropertyAliasMapping()
 74  0
   {
 75  0
   }
 76  
 
 77  
   // ****************************** Inner Classes *****************************
 78  
 
 79  
   // ********************************* Methods ********************************
 80  
 
 81  
   // --- init -----------------------------------------------------------------
 82  
 
 83  
   // --- get&set --------------------------------------------------------------
 84  
 
 85  
   // --- business -------------------------------------------------------------
 86  
 
 87  
   /**
 88  
    * Adds a new alias to the given physical resource.
 89  
    *
 90  
    * @param alias the new alias.
 91  
    * @param target the resource the alias refers to.
 92  
    * @throws BlankArgumentException if either {@code alias} or {@code target} is
 93  
    *           blank.
 94  
    * @throws DuplicateAliasException if there is already an alias registered
 95  
    *           that points to a different physical resource.
 96  
    */
 97  
   public void add(final String alias, final String target)
 98  
     throws BlankArgumentException, DuplicateAliasException
 99  
   {
 100  0
     Arg.checkNotBlank("alias", alias); // NOPMD
 101  0
     Arg.checkNotBlank("target", target);
 102  
 
 103  0
     final String oldTarget = aliasMapping.put(alias, target);
 104  0
     if (oldTarget != null && !oldTarget.equals(target))
 105  
     {
 106  0
       throw new DuplicateAliasException(alias, oldTarget, target);
 107  
     }
 108  0
   }
 109  
 
 110  
   /**
 111  
    * Checks if the given alias is registered.
 112  
    *
 113  
    * @param alias the alias to check.
 114  
    * @throws BlankArgumentException if {@code alias} is blank.
 115  
    * @return <code>true</code> if the alias is known to the system,
 116  
    *         <code>false</code> otherwise.
 117  
    */
 118  
   public boolean has(final String alias) throws BlankArgumentException
 119  
   {
 120  0
     Arg.checkNotBlank("alias", alias);
 121  
 
 122  0
     return aliasMapping.containsKey(alias);
 123  
   }
 124  
 
 125  
   /**
 126  
    * Returns the target the alias points to.
 127  
    *
 128  
    * @param alias the alias whose physical resource is requested.
 129  
    * @return the target the alias points to.
 130  
    * @throws BlankArgumentException if {@code alias} is blank.
 131  
    * @throws UnknownAliasException if the alias is not known.
 132  
    */
 133  
   public String get(final String alias) throws BlankArgumentException,
 134  
     UnknownAliasException
 135  
   {
 136  0
     Arg.checkNotBlank("alias", alias);
 137  
 
 138  0
     final String physical = aliasMapping.get(alias);
 139  0
     if (physical != null)
 140  
     {
 141  0
       return physical;
 142  
     }
 143  
 
 144  0
     throw new UnknownAliasException(alias);
 145  
   }
 146  
 
 147  
   /**
 148  
    * Traverses the registered aliases.
 149  
    *
 150  
    * @param traverser the traverser to use.
 151  
    * @throws NullPointerException if {@code traverser} is <code>null</code>.
 152  
    */
 153  
   public void traverse(final AliasTraverser traverser)
 154  
     throws NullPointerException
 155  
   {
 156  0
     Arg.checkNotNull("traverser", traverser);
 157  
 
 158  0
     for (final Entry<String, String> entry : aliasMapping.entrySet())
 159  
     {
 160  0
       final String alias = entry.getKey();
 161  0
       final String physical = entry.getValue();
 162  0
       traverser.handle(alias, physical);
 163  0
     }
 164  0
   }
 165  
 
 166  
   /**
 167  
    * Checks whether any aliases are registered.
 168  
    *
 169  
    * @return <code>false</code> if at least one alias is registered,
 170  
    *         <code>true</code> otherwise.
 171  
    */
 172  
   public boolean isEmpty()
 173  
   {
 174  0
     return aliasMapping.isEmpty();
 175  
   }
 176  
 
 177  
   // --- object basics --------------------------------------------------------
 178  
 
 179  
 }