View Javadoc

1   /*
2      Copyright 2007 Ivo Limmen
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 org.limmen.crs.model;
17  
18  import java.util.ArrayList;
19  import java.util.List;
20  
21  import org.limmen.crs.exception.SettingNotFoundException;
22  
23  /***
24   * The Section class is responsible for containing/grouping several related
25   * settings for a configuration.
26   *
27   * @author Ivo Limmen
28   * @see Setting
29   */
30  public class Section implements java.io.Serializable {
31  
32  	private static final long serialVersionUID = 3788064577454703625L;
33  
34  	private final List<Documentation> documentation = new ArrayList<Documentation>();
35  
36  	private String id;
37  
38  	private List<Setting> settings;
39  
40     Section(String id) {
41  
42  		this(id, new ArrayList<Setting>());
43  	}
44  
45  	Section(String id, List<Setting> settings) {
46  
47  		super();
48  
49  		setId(id);
50  		setSettings(settings);
51  	}
52  
53  	Documentation addDocumentation(Documentation documentation) {
54  
55  		getDocumentation().add(documentation);
56  
57  		return documentation;
58  	}
59  
60  	void addSetting(Setting newSetting) {
61  
62  		if (settingExists(newSetting.getId())) {
63  
64  			throw new IllegalArgumentException(
65  					String.format(
66  							"Setting '%0$s' allready exists in this section",
67  							newSetting.getId()));
68  		}
69  
70  		getSettings().add(newSetting);
71  	}
72  
73  	@Override
74     public boolean equals(Object obj) {
75  
76  		if (obj.getClass().equals(getClass())) {
77  
78  			Section other = (Section) obj;
79  
80  			if (getId().equals(other.getId())) {
81  
82  				if (getSettings().equals(other.getSettings())) {
83  
84  					return true;
85  				}
86  			}
87  		}
88  
89  		return false;
90  	}
91  
92  	List<Documentation> getDocumentation() {
93  
94  		return documentation;
95     }
96  
97  	public String getId() {
98  
99  		return this.id;
100 	}
101 
102 	public Setting getSetting(String aSettingId) throws SettingNotFoundException {
103 
104 		for (Setting setting : getSettings()) {
105 
106 			if (setting.getId().equals(aSettingId)) {
107 
108 				return setting;
109 			}
110 		}
111 
112 		throw new SettingNotFoundException("Setting '" + aSettingId + "' was not found.");
113 	}
114 
115 	public List<Setting> getSettings() {
116 
117 		return this.settings;
118 	}
119 
120 	@Override
121    public int hashCode() {
122 
123 	   final int prime = 31;
124 	   int result = 1;
125 	   result = prime * result + ((id == null) ? 0 : id.hashCode());
126 	   result = prime * result + ((settings == null) ? 0 : settings.hashCode());
127 	   return result;
128    }
129 
130    public void setId(String id) {
131 
132 		this.id = id;
133 	}
134 
135 	void setSettings(List<Setting> settings) {
136 
137 		this.settings = settings;
138 	}
139 
140 	/***
141 	 * Returns <code>true</code> when the specified setting id exists.
142 	 */
143 	private boolean settingExists(String settingId) {
144 
145 		for (Setting setting : getSettings()) {
146 
147 			if (setting.getId().equals(settingId)) {
148 
149 				return true;
150 			}
151 		}
152 
153 		return false;
154 	}
155 }