View Javadoc

1   /*
2    * Copyright 2006-2007 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package net.sf.ezmorph.primitive;
18  
19  import java.util.Locale;
20  
21  /**
22   * Base class por primitive integer conversion.
23   *
24   * @author Andres Almiray <aalmiray@users.sourceforge.net>
25   */
26  public abstract class AbstractIntegerMorpher extends AbstractPrimitiveMorpher
27  {
28     public AbstractIntegerMorpher()
29     {
30        super();
31     }
32  
33     /**
34      * @param useDefault if morph() should return a default value if the value to
35      *        be morphed is null
36      */
37     public AbstractIntegerMorpher( boolean useDefault )
38     {
39        super( useDefault );
40     }
41  
42     /**
43      * Trims the String from the begining to the first "."
44      */
45     protected String getIntegerValue( Object obj )
46     {
47        // use en_US Locale
48        Locale defaultLocale = Locale.getDefault();
49        String str = null;
50        try{
51           Locale.setDefault( Locale.US );
52           str = String.valueOf( obj );
53        }
54        finally{
55           Locale.setDefault( defaultLocale );
56        }
57  
58        int index = str.indexOf( "." );
59        if( index != -1 ){
60           str = str.substring( 0, index );
61        }
62        return str;
63     }
64  }