View Javadoc

1   /*
2    * Copyright 2012 The Netty Project
3    *
4    * The Netty Project licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. You may obtain a copy of the License at:
7    *
8    *   http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
15   */
16  package org.jboss.netty.handler.codec.http.multipart;
17  
18  import java.util.ArrayList;
19  import java.util.List;
20  
21  /**
22   * This Attribute is only for Encoder use to insert special command between object if needed
23   * (like Multipart Mixed mode)
24   */
25  public class InternalAttribute implements InterfaceHttpData {
26      protected List<String> value = new ArrayList<String>();
27  
28      public HttpDataType getHttpDataType() {
29          return HttpDataType.InternalAttribute;
30      }
31  
32      public List<String> getValue() {
33          return value;
34      }
35  
36      public void addValue(String value) {
37          if (value == null) {
38              throw new NullPointerException("value");
39          }
40          this.value.add(value);
41      }
42  
43      public void addValue(String value, int rank) {
44          if (value == null) {
45              throw new NullPointerException("value");
46          }
47          this.value.add(rank, value);
48      }
49  
50      public void setValue(String value, int rank) {
51          if (value == null) {
52              throw new NullPointerException("value");
53          }
54          this.value.set(rank, value);
55      }
56  
57      @Override
58      public int hashCode() {
59          return getName().hashCode();
60      }
61  
62      @Override
63      public boolean equals(Object o) {
64          if (!(o instanceof Attribute)) {
65              return false;
66          }
67          Attribute attribute = (Attribute) o;
68          return getName().equalsIgnoreCase(attribute.getName());
69      }
70  
71      public int compareTo(InterfaceHttpData data) {
72          if (!(data instanceof InternalAttribute)) {
73              throw new ClassCastException("Cannot compare " + getHttpDataType() +
74                      " with " + data.getHttpDataType());
75          }
76          return compareTo((InternalAttribute) data);
77      }
78  
79      public int compareTo(InternalAttribute o) {
80          return getName().compareToIgnoreCase(o.getName());
81      }
82  
83      public int size() {
84          int size = 0;
85          for (String elt : value) {
86              size += elt.length();
87          }
88          return size;
89      }
90      @Override
91      public String toString() {
92          StringBuilder result = new StringBuilder();
93          for (String elt : value) {
94              result.append(elt);
95          }
96          return result.toString();
97      }
98  
99      public String getName() {
100         return "InternalAttribute";
101     }
102 }