View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.syncope.client.console.commons;
20  
21  import java.util.ArrayList;
22  import java.util.Collection;
23  import java.util.Iterator;
24  import java.util.List;
25  import java.util.ListIterator;
26  import java.util.stream.Collectors;
27  import org.apache.commons.lang3.StringUtils;
28  import org.apache.syncope.common.lib.SyncopeConstants;
29  
30  public abstract class PropertyList<T> implements List<String> {
31  
32      @Override
33      public boolean add(final String item) {
34          final List<String> list = getEnumValuesAsList(getValues());
35          final boolean res = list.add(item);
36          setValues(list);
37          return res;
38      }
39  
40      @Override
41      public int size() {
42          return getEnumValuesAsList(getValues()).size();
43      }
44  
45      @Override
46      public boolean isEmpty() {
47          return getEnumValuesAsList(getValues()).isEmpty();
48      }
49  
50      @Override
51      public boolean contains(final Object o) {
52          return getEnumValuesAsList(getValues()).contains(o);
53      }
54  
55      @Override
56      public Iterator<String> iterator() {
57          return getEnumValuesAsList(getValues()).iterator();
58      }
59  
60      @Override
61      public Object[] toArray() {
62          throw new UnsupportedOperationException("Not supported yet.");
63      }
64  
65      @Override
66      public <T> T[] toArray(final T[] a) {
67          throw new UnsupportedOperationException("Not supported yet.");
68      }
69  
70      @Override
71      public boolean remove(final Object o) {
72          final List<String> list = getEnumValuesAsList(getValues());
73          final boolean res = list.remove(o);
74          setValues(list);
75          return res;
76      }
77  
78      @Override
79      public boolean containsAll(final Collection<?> c) {
80          return getEnumValuesAsList(getValues()).containsAll(c);
81      }
82  
83      @Override
84      public boolean addAll(final Collection<? extends String> c) {
85          final List<String> list = getEnumValuesAsList(getValues());
86          boolean res = list.addAll(c);
87          setValues(list);
88          return res;
89      }
90  
91      @Override
92      public boolean addAll(final int index, final Collection<? extends String> c) {
93          final List<String> list = getEnumValuesAsList(getValues());
94          final boolean res = list.addAll(index, c);
95          setValues(list);
96          return res;
97      }
98  
99      @Override
100     public boolean removeAll(final Collection<?> c) {
101         final List<String> list = getEnumValuesAsList(getValues());
102         final boolean res = list.removeAll(c);
103         setValues(list);
104         return res;
105     }
106 
107     @Override
108     public boolean retainAll(final Collection<?> c) {
109         final List<String> list = getEnumValuesAsList(getValues());
110         final boolean res = list.retainAll(c);
111         setValues(list);
112         return res;
113     }
114 
115     @Override
116     public void clear() {
117         final List<String> list = getEnumValuesAsList(getValues());
118         list.clear();
119         setValues(list);
120     }
121 
122     @Override
123     public String get(final int index) {
124         final List<String> list = getEnumValuesAsList(getValues());
125         return list.get(index);
126     }
127 
128     @Override
129     public String set(final int index, final String element) {
130         final List<String> list = getEnumValuesAsList(getValues());
131         final String res = list.set(index, element);
132         setValues(list);
133         return res;
134     }
135 
136     @Override
137     public void add(final int index, final String element) {
138         throw new UnsupportedOperationException("Not supported yet.");
139     }
140 
141     @Override
142     public String remove(final int index) {
143         final List<String> list = getEnumValuesAsList(getValues());
144         final String res = list.remove(index);
145         setValues(list);
146         return res;
147     }
148 
149     @Override
150     public int indexOf(final Object o) {
151         throw new UnsupportedOperationException("Not supported yet.");
152     }
153 
154     @Override
155     public int lastIndexOf(final Object o) {
156         throw new UnsupportedOperationException("Not supported yet.");
157     }
158 
159     @Override
160     public ListIterator<String> listIterator() {
161         throw new UnsupportedOperationException("Not supported yet.");
162     }
163 
164     @Override
165     public ListIterator<String> listIterator(final int index) {
166         throw new UnsupportedOperationException("Not supported yet.");
167     }
168 
169     @Override
170     public List<String> subList(final int fromIndex, final int toIndex) {
171         throw new UnsupportedOperationException("Not supported yet.");
172     }
173 
174     public static String getEnumValuesAsString(final List<String> enumerationValues) {
175         return enumerationValues.stream().
176                 map(v -> StringUtils.isEmpty(v) ? StringUtils.EMPTY : v.trim()).
177                 collect(Collectors.joining(SyncopeConstants.ENUM_VALUES_SEPARATOR));
178     }
179 
180     public static List<String> getEnumValuesAsList(final String enumerationValues) {
181         final List<String> values = new ArrayList<>();
182         if (StringUtils.isNotBlank(enumerationValues)) {
183             for (String value : enumerationValues.split(SyncopeConstants.ENUM_VALUES_SEPARATOR)) {
184                 values.add(value.trim());
185             }
186             if (enumerationValues.trim().endsWith(SyncopeConstants.ENUM_VALUES_SEPARATOR)) {
187                 values.add(StringUtils.EMPTY);
188             }
189         } else {
190             values.add(StringUtils.EMPTY);
191         }
192 
193         return values;
194     }
195 
196     public abstract String getValues();
197 
198     public abstract void setValues(List<String> list);
199 }