View Javadoc

1   package org.apache.onami.converters.system;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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   * Converter implementation for {@code java.util.BitSet}.
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       * {@inheritDoc}
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  }