Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||||||
ValuePart |
|
| 2.0;2 |
1 | // Copyright 2004, 2005 The Apache Software Foundation |
|
2 | // |
|
3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
|
4 | // you may not use this file except in compliance with the License. |
|
5 | // You may obtain a copy of the License at |
|
6 | // |
|
7 | // http://www.apache.org/licenses/LICENSE-2.0 |
|
8 | // |
|
9 | // Unless required by applicable law or agreed to in writing, software |
|
10 | // distributed under the License is distributed on an "AS IS" BASIS, |
|
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
12 | // See the License for the specific language governing permissions and |
|
13 | // limitations under the License. |
|
14 | ||
15 | package org.apache.tapestry.multipart; |
|
16 | ||
17 | import java.util.ArrayList; |
|
18 | import java.util.List; |
|
19 | ||
20 | /** |
|
21 | * A portion of a multipart request that stores a value, or values, for a |
|
22 | * parameter. |
|
23 | * |
|
24 | * @author Howard Lewis Ship |
|
25 | * @since 2.0.1 |
|
26 | */ |
|
27 | ||
28 | public class ValuePart |
|
29 | { |
|
30 | ||
31 | private int _count; |
|
32 | ||
33 | // Stores either String or List of String |
|
34 | private Object _value; |
|
35 | ||
36 | public ValuePart(String value) |
|
37 | 3 | { |
38 | 3 | _count = 1; |
39 | 3 | _value = value; |
40 | 3 | } |
41 | ||
42 | public int getCount() |
|
43 | { |
|
44 | 2 | return _count; |
45 | } |
|
46 | ||
47 | /** |
|
48 | * Returns the value, or the first value (if multi-valued). |
|
49 | */ |
|
50 | ||
51 | public String getValue() |
|
52 | { |
|
53 | 2 | if (_count == 1) return (String) _value; |
54 | ||
55 | 1 | List l = (List) _value; |
56 | ||
57 | 1 | return (String) l.get(0); |
58 | } |
|
59 | ||
60 | /** |
|
61 | * Returns the values as an array of strings. If there is only one value, it |
|
62 | * is returned wrapped as a single element array. |
|
63 | */ |
|
64 | ||
65 | public String[] getValues() |
|
66 | { |
|
67 | 3 | if (_count == 1) return new String[] { (String) _value }; |
68 | ||
69 | 2 | List l = (List) _value; |
70 | ||
71 | 2 | return (String[]) l.toArray(new String[_count]); |
72 | } |
|
73 | ||
74 | public void add(String newValue) |
|
75 | { |
|
76 | 3 | if (_count == 1) |
77 | { |
|
78 | 2 | List l = new ArrayList(); |
79 | 2 | l.add(_value); |
80 | 2 | l.add(newValue); |
81 | ||
82 | 2 | _value = l; |
83 | 2 | _count++; |
84 | 2 | return; |
85 | } |
|
86 | ||
87 | 1 | List l = (List) _value; |
88 | 1 | l.add(newValue); |
89 | 1 | _count++; |
90 | 1 | } |
91 | ||
92 | /** |
|
93 | * Does nothing. |
|
94 | */ |
|
95 | ||
96 | public void cleanup() |
|
97 | { |
|
98 | 0 | } |
99 | } |