1 package org.apache.onami.converters.system;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.util.BitSet;
23 import java.util.StringTokenizer;
24
25 import org.apache.onami.converters.core.AbstractConverter;
26 import org.kohsuke.MetaInfServices;
27
28 import com.google.inject.Module;
29 import com.google.inject.TypeLiteral;
30
31
32
33
34 @MetaInfServices( Module.class )
35 public final class BitSetConverter
36 extends AbstractConverter<BitSet>
37 {
38
39 private static final String DEFAULT_SEPARATOR = ",";
40
41 private static final int CHAR_LENGTH = 1;
42
43
44
45
46 public Object convert( String value, TypeLiteral<?> toType )
47 {
48 BitSet bitSet = new BitSet();
49
50 int currentIndex = 0;
51 StringTokenizer tokenizer = new StringTokenizer( value, DEFAULT_SEPARATOR );
52 while ( tokenizer.hasMoreTokens() )
53 {
54 String current = tokenizer.nextToken().trim();
55
56 if ( current.length() == 0 )
57 {
58 throw new IllegalArgumentException( "Input '" + value
59 + "' is not a valid java.util.BitSet, fragment at position " + currentIndex + " is empty" );
60 }
61
62 if ( CHAR_LENGTH == current.length() && !Character.isDigit( current.charAt( 0 ) ) )
63 {
64 bitSet.set( current.charAt( 0 ) );
65 }
66 else
67 {
68 for ( int i = 0; i < current.length(); i++ )
69 {
70 if ( !Character.isDigit( current.charAt( i ) ) )
71 {
72 throw new IllegalArgumentException( "Input '"
73 + value
74 + "' is not a valid java.util.BitSet, fragment '"
75 + current
76 + "' at position "
77 + currentIndex
78 + " is not a valid integer" );
79 }
80 }
81 bitSet.set( Integer.parseInt( current ) );
82 }
83
84 currentIndex++;
85 }
86
87 return bitSet;
88 }
89
90 }