|
|||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
ValuePart.java | 100% | 100% | 83.3% | 97% |
|
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 parameter. | |
22 | * | |
23 | * @author Howard Lewis Ship | |
24 | * @since 2.0.1 | |
25 | */ | |
26 | ||
27 | public class ValuePart | |
28 | { | |
29 | private int _count; | |
30 | ||
31 | // Stores either String or List of String | |
32 | private Object _value; | |
33 | ||
34 | 50 | public ValuePart(String value) |
35 | { | |
36 | 50 | _count = 1; |
37 | 50 | _value = value; |
38 | } | |
39 | ||
40 | 4 | public int getCount() |
41 | { | |
42 | 4 | return _count; |
43 | } | |
44 | ||
45 | /** | |
46 | * Returns the value, or the first value (if multi-valued). | |
47 | */ | |
48 | ||
49 | 4 | public String getValue() |
50 | { | |
51 | 4 | if (_count == 1) |
52 | 2 | return (String) _value; |
53 | ||
54 | 2 | List l = (List) _value; |
55 | ||
56 | 2 | return (String) l.get(0); |
57 | } | |
58 | ||
59 | /** | |
60 | * Returns the values as an array of strings. If there is only one value, it is returned wrapped | |
61 | * as a single element array. | |
62 | */ | |
63 | ||
64 | 50 | public String[] getValues() |
65 | { | |
66 | 50 | if (_count == 1) |
67 | 44 | return new String[] |
68 | { (String) _value }; | |
69 | ||
70 | 6 | List l = (List) _value; |
71 | ||
72 | 6 | return (String[]) l.toArray(new String[_count]); |
73 | } | |
74 | ||
75 | 8 | public void add(String newValue) |
76 | { | |
77 | 8 | if (_count == 1) |
78 | { | |
79 | 6 | List l = new ArrayList(); |
80 | 6 | l.add(_value); |
81 | 6 | l.add(newValue); |
82 | ||
83 | 6 | _value = l; |
84 | 6 | _count++; |
85 | 6 | return; |
86 | } | |
87 | ||
88 | 2 | List l = (List) _value; |
89 | 2 | l.add(newValue); |
90 | 2 | _count++; |
91 | } | |
92 | ||
93 | /** | |
94 | * Does nothing. | |
95 | */ | |
96 | ||
97 | 0 | public void cleanup() |
98 | { | |
99 | } | |
100 | } |
|