Skip to main content
summaryrefslogtreecommitdiffstats
blob: 9a1b5ce48c87f617244b72861f50be0b63a05506 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/*******************************************************************************
 * Copyright (c) 2013 Boeing.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Boeing - initial API and implementation
 *******************************************************************************/
package org.eclipse.osee.orcs.core.internal.util;

import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import org.eclipse.osee.framework.jdk.core.type.OseeCoreException;
import org.eclipse.osee.framework.jdk.core.type.ResultSet;
import com.google.common.base.Predicate;
import com.google.common.collect.LinkedHashMultimap;
import com.google.common.collect.Multimap;
import com.google.common.collect.Multimaps;

/**
 * @author Roberto E. Escobar
 */
public abstract class FilterableCollection<MATCH_DATA, KEY, DATA> {

   private final Multimap<KEY, DATA> map;

   protected FilterableCollection(Multimap<KEY, DATA> map) {
      super();
      this.map = map;
   }

   protected FilterableCollection() {
      this(Multimaps.synchronizedMultimap(LinkedHashMultimap.<KEY, DATA> create()));
   }

   protected abstract ResultSet<DATA> createResultSet(List<DATA> values);

   protected abstract <T extends DATA> ResultSet<T> createResultSet(KEY attributeType, List<T> values);

   public void add(KEY type, DATA data) {
      map.put(type, data);
   }

   public void remove(KEY type, DATA data) {
      map.remove(type, data);
   }

   public Collection<DATA> getAll() {
      return map.values();
   }

   public Collection<DATA> getAllByType(KEY type) {
      return map.get(type);
   }

   protected List<DATA> getListByFilter(Predicate<MATCH_DATA> matcher) throws OseeCoreException {
      return getListByFilter(getAll(), matcher);
   }

   protected ResultSet<DATA> getResultSetByFilter(Predicate<MATCH_DATA> matcher) throws OseeCoreException {
      return getResultSetByFilter(getAll(), matcher);
   }

   protected <T extends DATA> ResultSet<T> getSetByFilter(KEY type, Predicate<MATCH_DATA> matcher) throws OseeCoreException {
      List<T> result = getListByFilter(type, matcher);
      ResultSet<T> resultSet = createResultSet(type, result);
      return resultSet;
   }

   protected <T extends DATA> List<T> getListByFilter(KEY type, Predicate<MATCH_DATA> matcher) throws OseeCoreException {
      return getListByFilter(getAllByType(type), matcher);
   }

   protected boolean hasItemMatchingFilter(Predicate<MATCH_DATA> matcher) {
      return hasItemMatchingFilter(getAll(), matcher);
   }

   protected boolean hasItemMatchingFilter(KEY type, Predicate<MATCH_DATA> matcher) {
      return hasItemMatchingFilter(getAllByType(type), matcher);
   }

   private ResultSet<DATA> getResultSetByFilter(Collection<DATA> source, Predicate<MATCH_DATA> matcher) throws OseeCoreException {
      List<DATA> values = getListByFilter(source, matcher);
      return createResultSet(values);
   }

   @SuppressWarnings({"unchecked", "unused"})
   private <T extends DATA> List<T> getListByFilter(Collection<DATA> source, Predicate<MATCH_DATA> matcher) throws OseeCoreException {
      List<T> toReturn;
      if (source != null && !source.isEmpty()) {
         toReturn = new LinkedList<T>();
         for (Iterator<DATA> iterator = source.iterator(); iterator.hasNext();) {
            DATA data = iterator.next();
            if (isValid(data)) {
               MATCH_DATA toMatch = asMatcherData(data);
               if (matcher.apply(toMatch)) {
                  toReturn.add((T) data);
               }
            } else {
               iterator.remove();
            }
         }
      } else {
         toReturn = Collections.emptyList();
      }
      return toReturn;
   }

   private boolean hasItemMatchingFilter(Collection<DATA> source, Predicate<MATCH_DATA> matcher) {
      boolean result = false;
      if (source != null && !source.isEmpty()) {
         for (Iterator<DATA> iterator = source.iterator(); iterator.hasNext();) {
            DATA data = iterator.next();
            if (isValid(data)) {
               MATCH_DATA toMatch = asMatcherData(data);
               if (matcher.apply(toMatch)) {
                  result = true;
                  break;
               }
            } else {
               iterator.remove();
            }
         }
      }
      return result;
   }

   protected boolean isValid(DATA data) {
      return true;
   }

   protected abstract MATCH_DATA asMatcherData(DATA data);

   @Override
   public String toString() {
      return "FilterableCollection [map=" + map + "]";
   }
}

Back to the top