View Javadoc

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.spi.config.cache;
17  
18  import java.io.Serializable;
19  import java.util.concurrent.TimeUnit;
20  
21  /**
22   * The configuration for cache entries.
23   *
24   * @serial
25   */
26  public final class CacheEntryConfiguration implements Serializable
27  {
28    // ********************************* Fields *********************************
29  
30    // --- constants ------------------------------------------------------------
31  
32    /**
33     * The class version identifier.
34     */
35    private static final long serialVersionUID = 1L;
36  
37    /**
38     * The default max idle time in seconds.
39     */
40    private static final int DEFAULT_MAX_IDLE_TIME = -1;
41  
42    /**
43     * The default lifespan time in seconds.
44     */
45    private static final int DEFAULT_LIFESPAN_TIME = -1;
46  
47    /**
48     * Delimiter for the toString method.
49     */
50    private static final String TO_STRING_DELIMITER = ":";
51  
52    /**
53     * The enum entry for which the configuration shall be searched.
54     */
55    private final CacheEntryConfigurationType enumEntry;
56  
57    // --- members --------------------------------------------------------------
58  
59    // ****************************** Initializer *******************************
60  
61    // ****************************** Constructors ******************************
62  
63    /**
64     * Constructor to build an cache configuration for an entry.
65     *
66     * @param enumEntry the type of the entry.
67     */
68    public CacheEntryConfiguration(final CacheEntryConfigurationType enumEntry)
69    {
70      this.enumEntry = enumEntry;
71    }
72  
73    // ****************************** Inner Classes *****************************
74  
75    // ********************************* Methods ********************************
76  
77    // --- init -----------------------------------------------------------------
78  
79    // --- get&set --------------------------------------------------------------
80  
81    /**
82     * Returns the intendet lifespan of an entry.
83     *
84     * @return the lifespan.
85     */
86    public int getLifespan()
87    {
88      return getIntegerValue(enumEntry.systemPropertyLifespanName(),
89          DEFAULT_LIFESPAN_TIME);
90    }
91  
92    /**
93     * Returns the time unit {@link TimeUnit} for the intended lifespan of an
94     * entry.
95     *
96     * @return the time unit for the lifespan of an entry.
97     */
98    public TimeUnit getLifespanTimeUnit()
99    {
100     return getTimeUnitValue(enumEntry.systemPropertyLifespanTimeUnitName(),
101         TimeUnit.SECONDS);
102   }
103 
104   /**
105    * Returns the intended max idle time of an entry.
106    *
107    * @return the max idle time of an entry.
108    */
109   public int getMaxIdleTime()
110   {
111     return getIntegerValue(enumEntry.systemPropertyMayIdleTimeName(),
112         DEFAULT_MAX_IDLE_TIME);
113   }
114 
115   /**
116    * Returns the time unit {@link TimeUnit} for the max idle time of an entry.
117    *
118    * @return the max idle time time unit for an entry.
119    */
120   public TimeUnit getMaxIdleTimeTimeUnit()
121   {
122     return getTimeUnitValue(enumEntry.systemPropertyMaxIdleTimeTimeUnitName(),
123         TimeUnit.SECONDS);
124   }
125 
126   private int getIntegerValue(final String propertyName, final int defaultValue)
127   {
128     try
129     {
130       final int value =
131           Integer.parseInt(System.getProperty(propertyName,
132               String.valueOf(defaultValue)));
133       return value;
134     }
135     catch (final NumberFormatException e)
136     {
137       return defaultValue;
138     }
139   }
140 
141   private TimeUnit getTimeUnitValue(final String propertyName,
142       final TimeUnit defaultValue)
143   {
144     try
145     {
146       final String timeUnitValue =
147           System.getProperty(propertyName, defaultValue.toString());
148       final TimeUnit timeUnit = TimeUnit.valueOf(TimeUnit.class, timeUnitValue);
149       return timeUnit;
150     }
151     catch (final IllegalArgumentException e)
152     {
153       return defaultValue;
154     }
155   }
156 
157   // --- business -------------------------------------------------------------
158 
159   // --- object basics --------------------------------------------------------
160 
161   /**
162    * Returns the string representation of the object.
163    *
164    * @return the string representation of the object.
165    */
166   @Override
167   public String toString()
168   {
169     final StringBuilder buffer = new StringBuilder();
170     buffer.append("name:").append(TO_STRING_DELIMITER)
171         .append(enumEntry.toString()).append(TO_STRING_DELIMITER);
172     buffer.append("lifespan:").append(TO_STRING_DELIMITER)
173         .append(getLifespan()).append(TO_STRING_DELIMITER);
174     buffer.append("lifespan TimeUnit:").append(TO_STRING_DELIMITER)
175         .append(getLifespanTimeUnit()).append(TO_STRING_DELIMITER);
176     buffer.append("maxIdleTime:").append(TO_STRING_DELIMITER)
177         .append(getMaxIdleTime()).append(TO_STRING_DELIMITER);
178     buffer.append("maxIdelTime TimeUnit:").append(TO_STRING_DELIMITER)
179         .append(getMaxIdleTimeTimeUnit()).append(TO_STRING_DELIMITER);
180     return buffer.toString();
181   }
182 
183 }