James Moger
2011-12-28 abeaaf77673e9e764fe68e398eeda1e3c3c22ea5
commit | author | age
b75734 1 /*
JM 2  * Copyright 2011 gitblit.com.
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 com.gitblit.models;
17
18 import java.io.Serializable;
19 import java.util.ArrayList;
20 import java.util.List;
21
22 import com.gitblit.utils.StringUtils;
23
24 /**
25  * SettingModel represents a setting and all its metadata: name, current value,
26  * default value, description, and directives.
27  * 
28  * @author James Moger
29  */
30 public class SettingModel implements Serializable {
31
32     public static final String SPACE_DELIMITED = "SPACE-DELIMITED";
33
34     public static final String CASE_SENSITIVE = "CASE-SENSITIVE";
35
36     public static final String RESTART_REQUIRED = "RESTART REQUIRED";
37
38     public static final String SINCE = "SINCE";
39
40     public String name;
41     public volatile String currentValue;
42     public String defaultValue;
43     public String description;
44     public String since;
45     public boolean caseSensitive;
46     public boolean restartRequired;
47     public boolean spaceDelimited;
48
49     private static final long serialVersionUID = 1L;
50
51     public SettingModel() {
52     }
53
54     /**
55      * Returns true if the current value is the default value.
56      * 
57      * @return true if current value is the default value
58      */
59     public boolean isDefaultValue() {
60         return (currentValue != null && currentValue.equals(defaultValue))
61                 || currentValue.trim().length() == 0;
62     }
63
64     /**
65      * Returns the boolean value for the currentValue. If the currentValue can
66      * not be interpreted as a boolean, the defaultValue is returned.
67      * 
68      * @param defaultValue
69      * @return key value or defaultValue
70      */
71     public boolean getBoolean(boolean defaultValue) {
72         if (!StringUtils.isEmpty(currentValue)) {
73             return Boolean.parseBoolean(currentValue.trim());
74         }
75         return defaultValue;
76     }
77
78     /**
79      * Returns the integer value for the currentValue. If the currentValue can
80      * not be interpreted as an integer, the defaultValue is returned.
81      * 
82      * @param defaultValue
83      * @return key value or defaultValue
84      */
85     public int getInteger(int defaultValue) {
86         try {
87             if (!StringUtils.isEmpty(currentValue)) {
88                 return Integer.parseInt(currentValue.trim());
89             }
90         } catch (NumberFormatException e) {
91         }
92         return defaultValue;
93     }
94
95     /**
96      * Returns the char value for currentValue. If the currentValue can not be
97      * interpreted as a char, the defaultValue is returned.
98      * 
99      * @param defaultValue
100      * @return key value or defaultValue
101      */
102     public char getChar(char defaultValue) {
103         if (!StringUtils.isEmpty(currentValue)) {
104             return currentValue.trim().charAt(0);
105         }
106         return defaultValue;
107     }
108
109     /**
110      * Returns the string value for currentValue. If the currentValue is null,
111      * the defaultValue is returned.
112      * 
113      * @param defaultValue
114      * @return key value or defaultValue
115      */
116     public String getString(String defaultValue) {
117         if (currentValue != null) {
118             return currentValue.trim();
119         }
120         return defaultValue;
121     }
122
123     /**
124      * Returns a list of space-separated strings from the specified key.
125      * 
126      * @return list of strings
127      */
128     public List<String> getStrings() {
129         return getStrings(" ");
130     }
131
132     /**
133      * Returns a list of strings from the currentValue using the specified
134      * string separator.
135      * 
136      * @param separator
137      * @return list of strings
138      */
139     public List<String> getStrings(String separator) {
140         List<String> strings = new ArrayList<String>();
141         strings = StringUtils.getStringsFromValue(currentValue, separator);
142         return strings;
143     }
144 }