View Javadoc

1   package fr.in2p3.jsaga.impl.attributes;
2   
3   import fr.in2p3.jsaga.helpers.AttributeSerializer;
4   import fr.in2p3.jsaga.impl.monitoring.MetricMode;
5   import fr.in2p3.jsaga.impl.monitoring.MetricType;
6   import org.ogf.saga.error.*;
7   
8   /* ***************************************************
9   * *** Centre de Calcul de l'IN2P3 - Lyon (France) ***
10  * ***             http://cc.in2p3.fr/             ***
11  * ***************************************************
12  * File:   VectorAttributeImpl
13  * Author: Sylvain Reynaud (sreynaud@in2p3.fr)
14  * Date:   18 janv. 2008
15  * ***************************************************
16  * Description:                                      */
17  /**
18   *
19   */
20  public class VectorAttributeImpl<E> implements AttributeVector {
21      protected String m_key;
22      protected E[] m_objects;
23      private MetricMode m_mode;
24      private MetricType m_type;
25  
26      /** constructor for SAGA implementation */
27      public VectorAttributeImpl(String key, String desc, MetricMode mode, MetricType type, E[] initialObjects) {
28          m_key = key;
29          m_objects = initialObjects;
30          m_mode = mode;
31          m_type = type;
32      }
33  
34      /** clone */
35      public VectorAttributeImpl<E> clone() throws CloneNotSupportedException {
36          VectorAttributeImpl<E> clone = (VectorAttributeImpl<E>) super.clone();
37          clone.m_key = m_key;
38          clone.m_objects = m_objects;
39          clone.m_mode = m_mode;
40          clone.m_type = m_type;
41          return clone;
42      }
43  
44      ////////////////////////////////////// internal methods //////////////////////////////////////
45  
46      /** The SAGA engine implementation SHOULD use this method instead of method setValues. */
47      public void setObjects(E[] objects) {
48          m_objects = objects;
49      }
50  
51      /** The SAGA engine implementation SHOULD use this method instead of method getValues. */
52      public E[] getObjects() {
53          return m_objects;
54      }
55  
56      ////////////////////////////////////// interface Attribute //////////////////////////////////////
57  
58      public String getKey() {
59          return m_key;
60      }
61  
62      public MetricMode getMode() {
63      	return m_mode;
64      }
65      
66      public MetricType getType() {
67      	return m_type;
68      }
69      
70      public boolean isReadOnly() {
71          switch(m_mode) {
72              case ReadOnly:
73              case Final:
74                  return true;
75              case ReadWrite:
76              default:
77                  return false;
78          }
79      }
80  
81      public void setValue(String value) throws IncorrectStateException {
82          throw new IncorrectStateException("Attribute "+m_key+" not scalar");
83      }
84  
85      public String getValue() throws IncorrectStateException {
86          throw new IncorrectStateException("Attribute "+m_key+" not scalar");
87      }
88  
89      public void setValues(String[] values) throws NotImplementedException, IncorrectStateException, PermissionDeniedException {
90          AttributeSerializer<E> serializer = new AttributeSerializer<E>(m_type);
91          m_objects = (E[]) new Object[values!=null ? values.length : 0];
92          for (int i=0; i<m_objects.length; i++) {
93              try {
94                  m_objects[i] = serializer.fromString(values[i]);
95              } catch (DoesNotExistException e) {
96                  throw new NotImplementedException("INTERNAL ERROR: unexpected exception", e);
97              }
98          }
99      }
100 
101     public String[] getValues() throws NotImplementedException, IncorrectStateException, NoSuccessException {
102         AttributeSerializer<E> serializer = new AttributeSerializer<E>(m_type);
103         String[] values = new String[m_objects!=null ? m_objects.length : 0];
104         for (int i=0; i<values.length; i++) {
105             try {
106                 values[i] = serializer.toString(m_objects[i]);
107             } catch (DoesNotExistException e) {
108                 throw new NotImplementedException("INTERNAL ERROR: unexpected exception", e);
109             }
110         }
111         return values;
112     }
113 
114     public boolean equals(Object o) {
115         if (o instanceof VectorAttributeImpl) {
116             VectorAttributeImpl attribute = (VectorAttributeImpl) o;
117             if (m_key.equals(attribute.getKey())) {
118                 if (m_objects != null) {
119                     Object[] objects = attribute.getObjects();
120                     if (objects!=null && m_objects.length==objects.length) {
121                         for (int i=0; i<m_objects.length; i++) {
122                             if (!m_objects[i].equals(objects[i])) {
123                                 return false;
124                             }
125                         }
126                         return true;
127                     } else {
128                         return false;
129                     }
130                 } else {
131                     return attribute.getObjects() == null;
132                 }
133             } else {
134                 return false;
135             }
136         } else {
137             return false;
138         }
139     }
140 }