Coverage Report - de.smartics.properties.impl.config.domain.key.envapp.EnvAppConfigurationKeyFactory
 
Classes in this File Line Coverage Branch Coverage Complexity
EnvAppConfigurationKeyFactory
0%
0/22
0%
0/4
1.571
 
 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.impl.config.domain.key.envapp;
 17  
 
 18  
 import static org.apache.commons.lang.StringUtils.defaultIfBlank;
 19  
 import de.smartics.properties.api.config.domain.key.ApplicationId;
 20  
 import de.smartics.properties.api.config.domain.key.ConfigurationKey;
 21  
 import de.smartics.properties.api.config.domain.key.ConfigurationKeyFactory;
 22  
 import de.smartics.properties.api.config.domain.key.EnvironmentId;
 23  
 
 24  
 /**
 25  
  * Factory for configuration keys.
 26  
  */
 27  0
 final class EnvAppConfigurationKeyFactory implements
 28  
     ConfigurationKeyFactory<EnvAppConfigurationKey>
 29  
 {
 30  
   // ********************************* Fields *********************************
 31  
 
 32  
   // --- constants ------------------------------------------------------------
 33  
 
 34  
   /**
 35  
    * The default key referencing any environment and any application.
 36  
    */
 37  0
   private static final EnvAppConfigurationKey DEFAULT =
 38  
       new EnvAppConfigurationKey(EnvironmentId.ANY_ENV, ApplicationId.ANY_APP);
 39  
 
 40  
   // --- members --------------------------------------------------------------
 41  
 
 42  
   // ****************************** Initializer *******************************
 43  
 
 44  
   // ****************************** Constructors ******************************
 45  
 
 46  
   /**
 47  
    * Default constructor.
 48  
    */
 49  
   EnvAppConfigurationKeyFactory()
 50  0
   {
 51  0
   }
 52  
 
 53  
   // ****************************** Inner Classes *****************************
 54  
 
 55  
   // ********************************* Methods ********************************
 56  
 
 57  
   // --- init -----------------------------------------------------------------
 58  
 
 59  
   // --- get&set --------------------------------------------------------------
 60  
 
 61  
   // --- business -------------------------------------------------------------
 62  
 
 63  
   @Override
 64  
   public EnvAppConfigurationKey createDefaultKey()
 65  
   {
 66  0
     return DEFAULT;
 67  
   }
 68  
 
 69  
   @Override
 70  
   public ConfigurationKey<?> createKey(final ApplicationId applicationId)
 71  
   {
 72  0
     final ConfigurationKey<?> key =
 73  
         new EnvAppConfigurationKey(EnvironmentId.ANY_ENV, applicationId);
 74  0
     return key;
 75  
   }
 76  
 
 77  
   @Override
 78  
   public ConfigurationKey<?> createUniqueKey(final String id)
 79  
   {
 80  0
     return new EnvAppConfigurationKey(new EnvironmentId(id),
 81  
         ApplicationId.ANY_APP);
 82  
   }
 83  
 
 84  
   @Override
 85  
   public ConfigurationKey<?> createKey(final ConfigurationKey<?> key,
 86  
       final ApplicationId appId)
 87  
   {
 88  0
     final ConfigurationKey<?> newKey =
 89  
         new EnvAppConfigurationKey((EnvAppConfigurationKey) key, appId);
 90  0
     return newKey;
 91  
   }
 92  
 
 93  
   @Override
 94  
   public ConfigurationKey<?> createKeyFromString(final String key)
 95  
     throws IllegalArgumentException
 96  
   {
 97  
 
 98  0
     if (key == null)
 99  
     {
 100  0
       throw new IllegalArgumentException(String.format(
 101  
           "Cannot parse key '%s', since key is null", key));
 102  
 
 103  
     }
 104  0
     final String[] keyArray = key.split("/", -1);
 105  0
     final int count = keyArray.length;
 106  0
     final int expectedCount = 2;
 107  0
     if (count != expectedCount)
 108  
     {
 109  0
       throw new IllegalArgumentException(String.format(
 110  
           "Cannot parse key '%s', since %s slash-separated elements are expected,"
 111  
               + " but found only %s.", key, expectedCount, count));
 112  
     }
 113  
 
 114  0
     final EnvironmentId environmentId =
 115  
         EnvironmentId.valueOf(defaultIfBlank(keyArray[0], null));
 116  0
     final ApplicationId applicationId =
 117  
         ApplicationId.valueOf(defaultIfBlank(keyArray[1], null));
 118  
 
 119  0
     final ConfigurationKey<?> newKey =
 120  
         new EnvAppConfigurationKey(environmentId, applicationId);
 121  0
     return newKey;
 122  
   }
 123  
 
 124  
   @Override
 125  
   public ConfigurationKey<?> createStaticKey(final ConfigurationKey<?> key)
 126  
   {
 127  0
     return new EnvAppConfigurationKey((EnvAppConfigurationKey) key,
 128  
         ((EnvAppConfigurationKey) key).getApplicationId());
 129  
   }
 130  
 
 131  
   // --- object basics --------------------------------------------------------
 132  
 
 133  
 }