Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   *  Copyright 2001-2005 Stephen Colebourne
   *
   *  Licensed under the Apache License, Version 2.0 (the "License");
   *  you may not use this file except in compliance with the License.
   *  You may obtain a copy of the License at
   *
   *      http://www.apache.org/licenses/LICENSE-2.0
   *
  *  Unless required by applicable law or agreed to in writing, software
  *  distributed under the License is distributed on an "AS IS" BASIS,
  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  *  See the License for the specific language governing permissions and
  *  limitations under the License.
  */
 package org.goda.time.convert;

ConverterManager controls the date and time converters.

This class enables additional conversion classes to be added via addInstantConverter(org.goda.time.convert.InstantConverter), which may replace an existing converter. Similar methods exist for duration, time period and interval converters.

This class is threadsafe, so adding/removing converters can be done at any time. Updating the set of convertors is relatively expensive, and so should not be performed often.

The default instant converters are:

  • ReadableInstant
  • String
  • Calendar
  • Date (includes sql package subclasses)
  • Long (milliseconds)
  • null (now)
The default partial converters are:
  • ReadablePartial
  • ReadableInstant
  • String
  • Calendar
  • Date (includes sql package subclasses)
  • Long (milliseconds)
  • null (now)
The default duration converters are:
  • ReadableDuration
  • ReadableInterval
  • String
  • Long (milliseconds)
  • null (zero ms)
The default time period converters are:
  • ReadablePeriod
  • ReadableInterval
  • String
  • null (zero)
The default interval converters are:
  • ReadableInterval
  • String
  • null (zero-length from now to now)

Author(s):
Stephen Colebourne
Brian S O'Neill
Since:
1.0
 
 public final class ConverterManager {

    
Singleton instance, lazily loaded to avoid class loading.
 
     private static ConverterManager INSTANCE;
 
     public static ConverterManager getInstance() {
         if ( == null) {
              = new ConverterManager();
         }
         return ;
     }
     
     private ConverterSet iInstantConverters;
     private ConverterSet iPartialConverters;
     private ConverterSet iDurationConverters;
     private ConverterSet iPeriodConverters;
     private ConverterSet iIntervalConverters;
    
    
Restricted constructor.
    protected ConverterManager() {
        super();
         = new ConverterSet(new Converter[] {
            .,
            .,
            .,
            .,
            .,
        });
         = new ConverterSet(new Converter[] {
            .,
            .,
            .,
            .,
            .,
            .,
        });
         = new ConverterSet(new Converter[] {
            .,
            .,
            .,
            .,
            .,
        });
         = new ConverterSet(new Converter[] {
            .,
            .,
            .,
            .,
            .,
        });
         = new ConverterSet(new Converter[] {
            .,
            .,
            .,
        });
    }
    //-----------------------------------------------------------------------
    
Gets the best converter for the object specified.

Parameters:
object the object to convert
Returns:
the converter to use
Throws:
java.lang.IllegalArgumentException if no suitable converter
java.lang.IllegalStateException if multiple converters match the type equally well
    public InstantConverter getInstantConverter(Object object) {
        InstantConverter converter =
            (InstantConverter).select(object == null ? null : object.getClass());
        if (converter != null) {
            return converter;
        }
        throw new IllegalArgumentException("No instant converter found for type: " +
            (object == null ? "null" : object.getClass().getName()));
    }
    
    //-----------------------------------------------------------------------
    
Gets a copy of the set of converters.

Returns:
the converters, a copy of the real data, never null
        ConverterSet set = ;
        InstantConverter[] converters = new InstantConverter[set.size()];
        set.copyInto(converters);
        return converters;
    }
    
    
Adds a converter to the set of converters. If a matching converter is already in the set, the given converter replaces it. If the converter is exactly the same as one already in the set, no changes are made.

The order in which converters are added is not relevent. The best converter is selected by examining the object hierarchy.

Parameters:
converter the converter to add, null ignored
Returns:
replaced converter, or null
         //   throws SecurityException {
        
        //checkAlterInstantConverters();
        if (converter == null) {
            return null;
        }
        InstantConverter[] removed = new InstantConverter[1];
         = .add(converterremoved);
        return removed[0];
    }
    
    
Removes a converter from the set of converters. If the converter was not in the set, no changes are made.

Parameters:
converter the converter to remove, null ignored
Returns:
replaced converter, or null
            //throws SecurityException {
        
        //checkAlterInstantConverters();
        if (converter == null) {
            return null;
        }
        InstantConverter[] removed = new InstantConverter[1];
         = .remove(converterremoved);
        return removed[0];
    }
    
    
Checks whether the user has permission 'ConverterManager.alterInstantConverters'.

Throws:
java.lang.SecurityException if the user does not have the permission
    private void checkAlterInstantConverters(){// throws SecurityException {
//        SecurityManager sm = System.getSecurityManager();
//        if (sm != null) {
//            sm.checkPermission(new JodaTimePermission("ConverterManager.alterInstantConverters"));
//        }
    }
    //-----------------------------------------------------------------------
    
Gets the best converter for the object specified.

Parameters:
object the object to convert
Returns:
the converter to use
Throws:
java.lang.IllegalArgumentException if no suitable converter
java.lang.IllegalStateException if multiple converters match the type equally well
    public PartialConverter getPartialConverter(Object object) {
        PartialConverter converter =
            (PartialConverter).select(object == null ? null : object.getClass());
        if (converter != null) {
            return converter;
        }
        throw new IllegalArgumentException("No partial converter found for type: " +
            (object == null ? "null" : object.getClass().getName()));
    }
    
    //-----------------------------------------------------------------------
    
Gets a copy of the set of converters.

Returns:
the converters, a copy of the real data, never null
        ConverterSet set = ;
        PartialConverter[] converters = new PartialConverter[set.size()];
        set.copyInto(converters);
        return converters;
    }
    
    
Adds a converter to the set of converters. If a matching converter is already in the set, the given converter replaces it. If the converter is exactly the same as one already in the set, no changes are made.

The order in which converters are added is not relevent. The best converter is selected by examining the object hierarchy.

Parameters:
converter the converter to add, null ignored
Returns:
replaced converter, or null
            //throws SecurityException {
        
        //checkAlterPartialConverters();
        if (converter == null) {
            return null;
        }
        PartialConverter[] removed = new PartialConverter[1];
         = .add(converterremoved);
        return removed[0];
    }
    
    
Removes a converter from the set of converters. If the converter was not in the set, no changes are made.

Parameters:
converter the converter to remove, null ignored
Returns:
replaced converter, or null
           // throws SecurityException {
        
        //checkAlterPartialConverters();
        if (converter == null) {
            return null;
        }
        PartialConverter[] removed = new PartialConverter[1];
         = .remove(converterremoved);
        return removed[0];
    }
    
    
Checks whether the user has permission 'ConverterManager.alterPartialConverters'.

Throws:
java.lang.SecurityException if the user does not have the permission
    private void checkAlterPartialConverters() { // throws SecurityException {
//        SecurityManager sm = System.getSecurityManager();
//        if (sm != null) {
//            sm.checkPermission(new JodaTimePermission("ConverterManager.alterPartialConverters"));
//        }
    }
    //-----------------------------------------------------------------------
    
Gets the best converter for the object specified.

Parameters:
object the object to convert
Returns:
the converter to use
Throws:
java.lang.IllegalArgumentException if no suitable converter
java.lang.IllegalStateException if multiple converters match the type equally well
    public DurationConverter getDurationConverter(Object object) {
        DurationConverter converter =
            (DurationConverter).select(object == null ? null : object.getClass());
        if (converter != null) {
            return converter;
        }
        throw new IllegalArgumentException("No duration converter found for type: " +
            (object == null ? "null" : object.getClass().getName()));
    }
    
    //-----------------------------------------------------------------------
    
Gets a copy of the list of converters.

Returns:
the converters, a copy of the real data, never null
        ConverterSet set = ;
        DurationConverter[] converters = new DurationConverter[set.size()];
        set.copyInto(converters);
        return converters;
    }
    
    
Adds a converter to the set of converters. If a matching converter is already in the set, the given converter replaces it. If the converter is exactly the same as one already in the set, no changes are made.

The order in which converters are added is not relevent. The best converter is selected by examining the object hierarchy.

Parameters:
converter the converter to add, null ignored
Returns:
replaced converter, or null
            //throws SecurityException {
        
        //checkAlterDurationConverters();
        if (converter == null) {
            return null;
        }
        DurationConverter[] removed = new DurationConverter[1];
         = .add(converterremoved);
        return removed[0];
    }
    
    
Removes a converter from the set of converters. If the converter was not in the set, no changes are made.

Parameters:
converter the converter to remove, null ignored
Returns:
replaced converter, or null
           // throws SecurityException {
        
        //checkAlterDurationConverters();
        if (converter == null) {
            return null;
        }
        DurationConverter[] removed = new DurationConverter[1];
         = .remove(converterremoved);
        return removed[0];
    }
    
    
Checks whether the user has permission 'ConverterManager.alterDurationConverters'.

Throws:
java.lang.SecurityException if the user does not have the permission
    private void checkAlterDurationConverters() {// throws SecurityException {
//        SecurityManager sm = System.getSecurityManager();
//        if (sm != null) {
//            sm.checkPermission(new JodaTimePermission("ConverterManager.alterDurationConverters"));
//        }
    }
    //-----------------------------------------------------------------------
    
Gets the best converter for the object specified.

Parameters:
object the object to convert
Returns:
the converter to use
Throws:
java.lang.IllegalArgumentException if no suitable converter
java.lang.IllegalStateException if multiple converters match the type equally well
    public PeriodConverter getPeriodConverter(Object object) {
        PeriodConverter converter =
            (PeriodConverter).select(object == null ? null : object.getClass());
        if (converter != null) {
            return converter;
        }
        throw new IllegalArgumentException("No period converter found for type: " +
            (object == null ? "null" : object.getClass().getName()));
    }
    
    //-----------------------------------------------------------------------
    
Gets a copy of the list of converters.

Returns:
the converters, a copy of the real data, never null
    public PeriodConverter[] getPeriodConverters() {
        ConverterSet set = ;
        PeriodConverter[] converters = new PeriodConverter[set.size()];
        set.copyInto(converters);
        return converters;
    }
    
    
Adds a converter to the set of converters. If a matching converter is already in the set, the given converter replaces it. If the converter is exactly the same as one already in the set, no changes are made.

The order in which converters are added is not relevent. The best converter is selected by examining the object hierarchy.

Parameters:
converter the converter to add, null ignored
Returns:
replaced converter, or null
    public PeriodConverter addPeriodConverter(PeriodConverter converter) {
            //throws SecurityException {
        
        //checkAlterPeriodConverters();
        if (converter == null) {
            return null;
        }
        PeriodConverter[] removed = new PeriodConverter[1];
         = .add(converterremoved);
        return removed[0];
    }
    
    
Removes a converter from the set of converters. If the converter was not in the set, no changes are made.

Parameters:
converter the converter to remove, null ignored
Returns:
replaced converter, or null
            //hrows SecurityException {
        
        //checkAlterPeriodConverters();
        if (converter == null) {
            return null;
        }
        PeriodConverter[] removed = new PeriodConverter[1];
         = .remove(converterremoved);
        return removed[0];
    }
    
    
Checks whether the user has permission 'ConverterManager.alterPeriodConverters'.

Throws:
java.lang.SecurityException if the user does not have the permission
    private void checkAlterPeriodConverters() { //throws SecurityException {
//        SecurityManager sm = System.getSecurityManager();
//        if (sm != null) {
//            sm.checkPermission(new JodaTimePermission("ConverterManager.alterPeriodConverters"));
//        }
    }
    //-----------------------------------------------------------------------
    
Gets the best converter for the object specified.

Parameters:
object the object to convert
Returns:
the converter to use
Throws:
java.lang.IllegalArgumentException if no suitable converter
java.lang.IllegalStateException if multiple converters match the type equally well
    public IntervalConverter getIntervalConverter(Object object) {
        IntervalConverter converter =
            (IntervalConverter).select(object == null ? null : object.getClass());
        if (converter != null) {
            return converter;
        }
        throw new IllegalArgumentException("No interval converter found for type: " +
            (object == null ? "null" : object.getClass().getName()));
    }
    
    //-----------------------------------------------------------------------
    
Gets a copy of the list of converters.

Returns:
the converters, a copy of the real data, never null
        ConverterSet set = ;
        IntervalConverter[] converters = new IntervalConverter[set.size()];
        set.copyInto(converters);
        return converters;
    }
    
    
Adds a converter to the set of converters. If a matching converter is already in the set, the given converter replaces it. If the converter is exactly the same as one already in the set, no changes are made.

The order in which converters are added is not relevent. The best converter is selected by examining the object hierarchy.

Parameters:
converter the converter to add, null ignored
Returns:
replaced converter, or null
           // throws SecurityException {
        
        //checkAlterIntervalConverters();
        if (converter == null) {
            return null;
        }
        IntervalConverter[] removed = new IntervalConverter[1];
         = .add(converterremoved);
        return removed[0];
    }
    
    
Removes a converter from the set of converters. If the converter was not in the set, no changes are made.

Parameters:
converter the converter to remove, null ignored
Returns:
replaced converter, or null
            //throws SecurityException {
        
        //checkAlterIntervalConverters();
        if (converter == null) {
            return null;
        }
        IntervalConverter[] removed = new IntervalConverter[1];
         = .remove(converterremoved);
        return removed[0];
    }
    
    
Checks whether the user has permission 'ConverterManager.alterIntervalConverters'.

Throws:
java.lang.SecurityException if the user does not have the permission
    private void checkAlterIntervalConverters(){ // throws SecurityException {
//        SecurityManager sm = System.getSecurityManager();
//        if (sm != null) {
//            sm.checkPermission(new JodaTimePermission("ConverterManager.alterIntervalConverters"));
//        }
    }
    //-----------------------------------------------------------------------
    
Gets a debug representation of the object.
    public String toString() {
        return "ConverterManager[" +
            .size() + " instant," +
            .size() + " partial," +
            .size() + " duration," +
            .size() + " period," +
            .size() + " interval]";
    }
New to GrepCode? Check out our FAQ X