|
|||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
ArrayData.java | 30% | 46.4% | 80% | 46.5% |
|
1 |
/*
|
|
2 |
|
|
3 |
VRMoo Common - Virtual Reality Object Oriented MUD Common Code
|
|
4 |
Copyright (C) 2003 VRMoo Development Team
|
|
5 |
|
|
6 |
|
|
7 |
This program is free software; you can redistribute it and/or modify
|
|
8 |
it under the terms of the GNU General Public License as published by
|
|
9 |
the Free Software Foundation; either version 2 of the License, or
|
|
10 |
(at your option) any later version.
|
|
11 |
|
|
12 |
This program is distributed in the hope that it will be useful,
|
|
13 |
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
14 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
15 |
GNU General Public License for more details.
|
|
16 |
|
|
17 |
You should have received a copy of the GNU General Public License
|
|
18 |
along with this program; if not, write to the Free Software
|
|
19 |
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
20 |
|
|
21 |
|
|
22 |
For information about VRMoo and its authors, please visit the website:
|
|
23 |
http://www.vrmoo.org/
|
|
24 |
|
|
25 |
*/
|
|
26 |
|
|
27 |
package org.vrmoo.common.data;
|
|
28 |
|
|
29 |
import java.util.ArrayList;
|
|
30 |
import java.util.List;
|
|
31 |
|
|
32 |
import org.vrmoo.common.exception.VRMooParseException;
|
|
33 |
|
|
34 |
/**
|
|
35 |
* Abstract class that contains the common functionality for dealing with an
|
|
36 |
* array of items.
|
|
37 |
*
|
|
38 |
* @author Jeff Weston
|
|
39 |
*/
|
|
40 |
public abstract class ArrayData |
|
41 |
{ |
|
42 |
/**
|
|
43 |
* The list of items.
|
|
44 |
*/
|
|
45 |
private List dataList;
|
|
46 |
|
|
47 |
/**
|
|
48 |
* Construct an instance of this class with no data initially.
|
|
49 |
*/
|
|
50 | 10 |
public ArrayData( )
|
51 |
{ |
|
52 | 10 |
dataList = new ArrayList( );
|
53 |
} |
|
54 |
|
|
55 |
/**
|
|
56 |
* Construct an instance of this class using text representing an encoded
|
|
57 |
* list of data.
|
|
58 |
*
|
|
59 |
* @param data the encoded list of data
|
|
60 |
*
|
|
61 |
* @throws VRMooParseException for any parsing errors that occur
|
|
62 |
*/
|
|
63 | 2 |
public ArrayData( String data ) throws VRMooParseException |
64 |
{ |
|
65 | 2 |
data = data.trim( ); |
66 | 2 |
dataList = new ArrayList( );
|
67 | 2 |
if ( data.indexOf( getDataType( ) ) != 0 )
|
68 |
{ |
|
69 | 0 |
throw new VRMooParseException( "Invalid data type encountered. " + |
70 |
"Expected " + getDataType( ) + "." ); |
|
71 |
} |
|
72 | 2 |
parseArray( data.substring( data.indexOf( ":" ) + 1 ) );
|
73 |
} |
|
74 |
|
|
75 |
/**
|
|
76 |
* Get the list of items.
|
|
77 |
*
|
|
78 |
* @return the list of items
|
|
79 |
*/
|
|
80 | 0 |
public List getDataList( )
|
81 |
{ |
|
82 | 0 |
return dataList;
|
83 |
} |
|
84 |
|
|
85 |
/**
|
|
86 |
* Convert the list of data to its encoded <code>String</code>
|
|
87 |
* representation.
|
|
88 |
*
|
|
89 |
* @return the encoded <code>String</code>
|
|
90 |
*/
|
|
91 | 12 |
public String toEncodedString( )
|
92 |
{ |
|
93 | 12 |
StringBuffer result = new StringBuffer( );
|
94 | 12 |
result.append( getDataType( ) ); |
95 | 12 |
result.append( ": " );
|
96 |
|
|
97 | 12 |
for ( int i = 0; i < dataList.size( ); i++ ) |
98 |
{ |
|
99 | 0 |
ItemData itemData = ( ItemData ) dataList.get( i ); |
100 | 0 |
if ( i != 0 )
|
101 |
{ |
|
102 | 0 |
result.append( ", " );
|
103 |
} |
|
104 | 0 |
result.append( "[ " );
|
105 | 0 |
result.append( itemData.toEncodedString( ) ); |
106 | 0 |
result.append( " ]" );
|
107 |
} |
|
108 | 12 |
return result.toString( );
|
109 |
} |
|
110 |
|
|
111 |
/**
|
|
112 |
* Get the data type we are parsing from the subclass.
|
|
113 |
*
|
|
114 |
* @return the data type we are parsing
|
|
115 |
*/
|
|
116 |
protected abstract String getDataType( );
|
|
117 |
|
|
118 |
/**
|
|
119 |
* This method delegates the parsing of a single item to a subclass.
|
|
120 |
*
|
|
121 |
* @param data the data for the single item to parse
|
|
122 |
*
|
|
123 |
* @return the parsed item
|
|
124 |
*
|
|
125 |
* @throws VRMooParseException for any parsing errors that occur
|
|
126 |
*/
|
|
127 |
protected abstract ItemData parseItem( String data )
|
|
128 |
throws VRMooParseException;
|
|
129 |
|
|
130 |
/**
|
|
131 |
* Parse an encoded array of items.
|
|
132 |
*
|
|
133 |
* @param data the text to parse
|
|
134 |
*
|
|
135 |
* @throws VRMooParseException for any parsing errors that occur
|
|
136 |
*/
|
|
137 | 2 |
private void parseArray( String data ) throws VRMooParseException |
138 |
{ |
|
139 | 2 |
data = data.trim( ); |
140 | 2 |
int itemEnd = data.indexOf( "]" ); |
141 | 2 |
while ( ( data.indexOf( "[" ) == 0 ) && ( itemEnd != -1 ) ) |
142 |
{ |
|
143 | 0 |
String item = data.substring( 1, itemEnd ); |
144 | 0 |
int nextIndex = data.indexOf( "[", itemEnd ); |
145 | 0 |
if ( nextIndex != -1 )
|
146 |
{ |
|
147 | 0 |
data = data.substring( nextIndex ).trim( ); |
148 |
} |
|
149 |
else
|
|
150 |
{ |
|
151 | 0 |
data = "";
|
152 |
} |
|
153 | 0 |
itemEnd = data.indexOf( "]" );
|
154 |
|
|
155 | 0 |
dataList.add( parseItem( item ) ); |
156 |
} |
|
157 |
} |
|
158 |
} |
|
159 |
|
|