View Javadoc
1   package org.nuiton.eugene.models.object.reader.yaml;
2   
3   /*
4    * #%L
5    * EUGene :: EUGene
6    * %%
7    * Copyright (C) 2004 - 2013 CodeLutin
8    * %%
9    * This program is free software: you can redistribute it and/or modify
10   * it under the terms of the GNU Lesser General Public License as 
11   * published by the Free Software Foundation, either version 3 of the 
12   * License, or (at your option) any later version.
13   * 
14   * This program is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17   * GNU General Lesser Public License for more details.
18   * 
19   * You should have received a copy of the GNU General Lesser Public 
20   * License along with this program.  If not, see
21   * <http://www.gnu.org/licenses/lgpl-3.0.html>.
22   * #L%
23   */
24  
25  import java.util.LinkedHashMap;
26  import java.util.LinkedList;
27  import java.util.List;
28  import java.util.Map;
29  
30  /**
31   * User: agiraudet
32   * Date: 28/05/13
33   * Time: 16:41
34   *
35   * le YamlObject est un objet intermédiaire et très laxiste, pouvant s'adapter à d'éventuelles évolutions
36   * (en cas d'évolutions de la syntaxe YAML ou de l'ObjectModel, les modifications permettant de conserver les interactions seront légères)
37   */
38  public class YamlObject {
39  
40      protected Map<String, List<String>> mapStringListString;
41  
42      protected Map<String, List<YamlObject>> mapStringListYamlObject;
43      //TODO: améliorer noms méthodes
44  
45      public YamlObject() {
46          mapStringListString = new LinkedHashMap<>();
47          mapStringListYamlObject = new LinkedHashMap<>();
48      }
49  
50      public void addYamlObjectToMap(String key, YamlObject value) {
51          if (mapStringListYamlObject.containsKey(key)) {
52              mapStringListYamlObject.get(key).add(value);
53          } else {
54              List<YamlObject> tmp = new LinkedList<>();
55              tmp.add(value);
56              mapStringListYamlObject.put(key, tmp);
57          }
58      }
59  
60      public void addStringToMap(String key, String value) {
61          if (mapStringListString.containsKey(key)) {
62              mapStringListString.get(key).add(value);
63          } else {
64              List<String> tmp = new LinkedList<>();
65              tmp.add(value);
66              mapStringListString.put(key, tmp);
67          }
68      }
69  
70      public YamlObject getFirstMapStringListYamlObject(String key) {
71          if (mapStringListYamlObject.containsKey(key)) {
72              if (!mapStringListYamlObject.isEmpty()) {
73                  return mapStringListYamlObject.get(key).get(0);
74              }
75          }
76          return null;
77      }
78  
79      public String getFirstMapStringListString(String key) {
80          if (mapStringListString.containsKey(key)) {
81              if (!mapStringListString.isEmpty()) {
82                  return mapStringListString.get(key).get(0);
83              }
84          }
85          return null;
86      }
87  
88      public boolean isUniqueMapStringListYamlObject(String key) {
89          return (sizeOfMapStringListYamlObject(key) == 1);
90      }
91  
92      public boolean isUniqueMapStringListString(String key) {
93          return (sizeOfMapStringListString(key) == 1);
94      }
95  
96      public boolean containsKeyYamlMapStringListYamlObject(String key) {
97          return mapStringListYamlObject.containsKey(key);
98      }
99  
100     public boolean containsKeyMapStringListString(String key) {
101         return mapStringListString.containsKey(key);
102     }
103 
104     public List<YamlObject> getMapStringListYamlObject(String key) {
105         if (mapStringListYamlObject.containsKey(key)) {
106             if (!mapStringListYamlObject.isEmpty()) {
107                 return mapStringListYamlObject.get(key);
108             }
109         }
110         //return null;//permet parcours
111         return new LinkedList<>();
112     }
113 
114     public List<String> getMapStringListString(String key) {
115         if (mapStringListString.containsKey(key)) {
116             if (!mapStringListString.isEmpty()) {
117                 return mapStringListString.get(key);
118             }
119         }
120         //return null;//permet parcours
121         return new LinkedList<>();
122     }
123 
124     public int sizeOfMapStringListYamlObject(String key) {
125         if (mapStringListYamlObject.containsKey(key)) {
126             return mapStringListYamlObject.get(key).size();
127         }
128         return 0;//-1
129     }
130 
131     public int sizeOfMapStringListString(String key) {
132         if (mapStringListString.containsKey(key)) {
133             return mapStringListString.get(key).size();
134         }
135         return 0;//-1
136     }
137 
138     public Map<String, List<YamlObject>> getMapStringListYamlObject() {
139         return mapStringListYamlObject;
140     }
141 
142     public Map<String, List<String>> getMapStringListString() {
143         return mapStringListString;
144     }
145 
146     public boolean setMapStringListString(String key, String value, String element) {
147         if (mapStringListString.containsKey(key)) {
148             if (mapStringListString.get(key).contains(value)) {
149                 mapStringListString.get(key).set(mapStringListString.get(key).indexOf(value), element);
150                 return true;
151             }
152         }
153         return false;
154     }
155 
156     public boolean setMapStringListYamlObject(String key, YamlObject value, YamlObject element) {
157         if (mapStringListYamlObject.containsKey(key)) {
158             if (mapStringListYamlObject.get(key).contains(value)) {
159                 mapStringListYamlObject.get(key).set(mapStringListYamlObject.get(key).indexOf(value), element);
160                 return true;
161             }
162         }
163         return false;
164     }
165 
166     public boolean removeMapStringListString(String key, String value) {
167         if (mapStringListString.containsKey(key)) {
168             return mapStringListString.get(key).remove(value);
169         }
170         return false;
171     }
172 
173     public boolean removeMapStringString(String key) {
174         if (mapStringListString.containsKey(key)) {
175             mapStringListString.remove(key);
176             return true;
177         }
178         return false;
179     }
180 
181     public boolean removeMapStringListYamlObject(String key, YamlObject value) {
182         if (mapStringListYamlObject.containsKey(key)) {
183             return mapStringListYamlObject.get(key).remove(value);
184         }
185         return false;
186     }
187 
188     public boolean removeMapStringListYamlObject(String key) {
189         if (mapStringListYamlObject.containsKey(key)) {
190             mapStringListYamlObject.remove(key);
191             return true;
192         }
193         return false;
194     }
195 
196     @Override
197     public boolean equals(Object o) {
198         if (o instanceof YamlObject) {
199             return (mapStringListYamlObject.equals(((YamlObject) o).getMapStringListYamlObject()) && mapStringListString.equals(((YamlObject) o).getMapStringListString()));
200         }
201         return false;
202     }
203 
204     @Override
205     public String toString() {
206         return toString("- ");
207     }
208 
209     public String toString(String indentation) {
210         StringBuilder res = new StringBuilder();
211 
212         for (Map.Entry<String, List<String>> entry : mapStringListString.entrySet()) {
213             for (String str : entry.getValue()) {
214                 res.append(indentation).append(entry.getKey()).append(": ").append(str).append("\n");
215             }
216         }
217         for (Map.Entry<String, List<YamlObject>> entry : mapStringListYamlObject.entrySet()) {
218             for (YamlObject yobj : entry.getValue()) {
219                 if (yobj != null) {
220                     res.append(indentation).append(entry.getKey()).append(":\n").append(yobj.toString("  " + indentation)).append("\n");
221                 }
222             }
223         }
224         return res.toString();
225     }
226 }