Skip to main content
summaryrefslogtreecommitdiffstats
blob: 29d58aedf89aa188efdb885306e657d34af0ee74 (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
144
145
146
147
148
149
150
151
152
153
154
/*******************************************************************************
 * Copyright (c) 2004, 2007 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.framework.ui.skynet.blam;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import org.eclipse.osee.framework.core.data.IArtifactType;
import org.eclipse.osee.framework.core.exception.OseeArgumentException;
import org.eclipse.osee.framework.core.model.Branch;
import org.eclipse.osee.framework.core.model.type.ArtifactType;
import org.eclipse.osee.framework.core.model.type.AttributeType;
import org.eclipse.osee.framework.skynet.core.User;
import org.eclipse.osee.framework.skynet.core.artifact.Artifact;

/**
 * @author Ryan D. Brooks
 */
public class VariableMap {
   private final Map<String, Object> variableMap = new HashMap<String, Object>();

   public VariableMap() {
      // provides a constructor that does not throw OseeArgumentException
   }

   public VariableMap(Object... optionArgs) throws OseeArgumentException {
      setValues(optionArgs);
   }

   public void setValues(Object... optionArgs) throws OseeArgumentException {
      for (int i = 0; i < optionArgs.length; i += 2) {
         Object object = optionArgs[i];
         if (object instanceof String) {
            variableMap.put((String) object, optionArgs[i + 1]);
         } else if (object == null) {
            throw new OseeArgumentException("The [%d]th option must not be null", i);
         } else {
            throw new OseeArgumentException("The [%d]th option must be of type string but is of type [%s]", i,
               object.getClass().getName());
         }
      }
   }

   public Object[] getValues() {
      Object[] values = new Object[variableMap.size() * 2];
      int index = 0;
      for (Entry<String, Object> entry : variableMap.entrySet()) {
         values[index++] = entry.getKey();
         values[index++] = entry.getValue();
      }
      return values;
   }

   public void setValue(String variableName, Object value) {
      variableMap.put(variableName, value);
   }

   public IArtifactType getArtifactType(String parameterName) throws OseeArgumentException {
      return getSingleCollectionValue(ArtifactType.class, parameterName);
   }

   public Artifact getArtifact(String parameterName) throws OseeArgumentException {
      Object object = variableMap.get(parameterName);
      if (object instanceof Artifact) {
         return (Artifact) object;
      }
      return getSingleCollectionValue(Artifact.class, parameterName);
   }

   public AttributeType getAttributeType(String parameterName) throws OseeArgumentException {
      return getSingleCollectionValue(AttributeType.class, parameterName);
   }

   public List<AttributeType> getAttributeTypes(String parameterName) throws OseeArgumentException {
      Collection<AttributeType> attrTypes = getCollection(AttributeType.class, parameterName);
      if (attrTypes == null) {
         return new ArrayList<AttributeType>();
      }
      return new ArrayList<AttributeType>(attrTypes);
   }

   public String getString(String parameterName) throws OseeArgumentException {
      return getValue(String.class, parameterName);
   }

   public Branch getBranch(String parameterName) throws OseeArgumentException {
      return getValue(Branch.class, parameterName);
   }

   public boolean getBoolean(String parameterName) throws OseeArgumentException {
      Boolean value = getValue(Boolean.class, parameterName);
      return value != null ? value : false;
   }

   @SuppressWarnings("unchecked")
   public <T> Collection<T> getCollection(Class<T> clazz, String parameterName) throws OseeArgumentException {
      List<T> results = new ArrayList<T>();
      for (Object obj : getValue(Collection.class, parameterName)) {
         if (clazz.isInstance(obj)) {
            results.add((T) obj);
         }
      }
      return results;
   }

   public User getUser(String parameterName) throws OseeArgumentException {
      return getValue(User.class, parameterName);
   }

   public List<Artifact> getArtifacts(String parameterName) throws OseeArgumentException {
      Collection<Artifact> artiafcts = getCollection(Artifact.class, parameterName);
      if (artiafcts == null) {
         return new ArrayList<Artifact>();
      }
      return new ArrayList<Artifact>(artiafcts);
   }

   private <T> T getSingleCollectionValue(Class<T> clazz, String parameterName) throws OseeArgumentException {
      Collection<T> objects = getCollection(clazz, parameterName);
      if (objects.size() != 1) {
         throw new OseeArgumentException("Require a collection of size 1 not %d", objects.size());
      }
      return objects.iterator().next();
   }

   private <T> T getValue(Class<T> clazz, String variableName) throws OseeArgumentException {
      Object value = variableMap.get(variableName);

      if (value != null && !clazz.isInstance(value)) {
         throw new OseeArgumentException("Expecting object of type [%s] not [%s]", clazz, value.getClass());
      }
      return clazz.cast(value);
   }

   public Object getValue(String variableName) {
      return variableMap.get(variableName);
   }

   @Override
   public String toString() {
      return variableMap.toString();
   }
}

Back to the top