Coverage Report - org.apache.commons.convert.NetConverters
 
Classes in this File Line Coverage Branch Coverage Complexity
NetConverters
100%
12/12
N/A
2.308
NetConverters$InetAddressToString
83%
5/6
50%
1/2
2.308
NetConverters$StringToInetAddress
66%
4/6
N/A
2.308
NetConverters$StringToURI
66%
4/6
N/A
2.308
NetConverters$StringToURL
66%
4/6
N/A
2.308
NetConverters$URIToURL
66%
4/6
N/A
2.308
NetConverters$URLToURI
66%
4/6
N/A
2.308
 
 1  
 /*******************************************************************************
 2  
  * Licensed to the Apache Software Foundation (ASF) under one
 3  
  * or more contributor license agreements.  See the NOTICE file
 4  
  * distributed with this work for additional information
 5  
  * regarding copyright ownership.  The ASF licenses this file
 6  
  * to you under the Apache License, Version 2.0 (the
 7  
  * "License"); you may not use this file except in compliance
 8  
  * with the License.  You may obtain a copy of the License at
 9  
  *
 10  
  * http://www.apache.org/licenses/LICENSE-2.0
 11  
  *
 12  
  * Unless required by applicable law or agreed to in writing,
 13  
  * software distributed under the License is distributed on an
 14  
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 15  
  * KIND, either express or implied.  See the License for the
 16  
  * specific language governing permissions and limitations
 17  
  * under the License.
 18  
  *******************************************************************************/
 19  
 package org.apache.commons.convert;
 20  
 
 21  
 import java.io.IOException;
 22  
 import java.net.InetAddress;
 23  
 import java.net.MalformedURLException;
 24  
 import java.net.URI;
 25  
 import java.net.URISyntaxException;
 26  
 import java.net.URL;
 27  
 
 28  
 /** java.net Converter classes. */
 29  2
 public class NetConverters implements ConverterLoader {
 30  
 
 31  
     public void loadConverters() {
 32  2
         Converters.loadContainedConverters(NetConverters.class);
 33  2
         Converters.registerConverter(new GenericToStringConverter<URI>(URI.class));
 34  2
         Converters.registerConverter(new GenericToStringConverter<URL>(URL.class));
 35  2
         Converters.registerConverter(new GenericSingletonToList<InetAddress>(InetAddress.class));
 36  2
         Converters.registerConverter(new GenericSingletonToList<URI>(URI.class));
 37  2
         Converters.registerConverter(new GenericSingletonToList<URL>(URL.class));
 38  2
         Converters.registerConverter(new GenericSingletonToSet<InetAddress>(InetAddress.class));
 39  2
         Converters.registerConverter(new GenericSingletonToSet<URI>(URI.class));
 40  2
         Converters.registerConverter(new GenericSingletonToSet<URL>(URL.class));
 41  2
     }
 42  
 
 43  
     /**
 44  
      * An object that converts an <code>InetAddress</code> to a
 45  
      * <code>String</code>.
 46  
      */
 47  1
     public static class InetAddressToString extends AbstractConverter<InetAddress, String> {
 48  
         public InetAddressToString() {
 49  3
             super(InetAddress.class, String.class);
 50  3
         }
 51  
 
 52  
         public String convert(InetAddress obj) throws ConversionException {
 53  1
             String hostName = obj.getHostName();
 54  1
             if (hostName != null) return hostName;
 55  0
             return obj.getHostAddress();
 56  
         }
 57  
     }
 58  
 
 59  
     /**
 60  
      * An object that converts a <code>String</code> to an
 61  
      * <code>InetAddress</code>.
 62  
      */
 63  1
     public static class StringToInetAddress extends AbstractConverter<String, InetAddress> {
 64  
         public StringToInetAddress() {
 65  2
             super(String.class, InetAddress.class);
 66  2
         }
 67  
 
 68  
         public InetAddress convert(String obj) throws ConversionException {
 69  
             try {
 70  1
                 return InetAddress.getByName(obj);
 71  0
             } catch (IOException e) {
 72  0
                 throw (ConversionException) new ConversionException(e.getMessage()).initCause(e);
 73  
             }
 74  
         }
 75  
     }
 76  
 
 77  
     /**
 78  
      * An object that converts a <code>String</code> to a
 79  
      * <code>URI</code>.
 80  
      */
 81  1
     public static class StringToURI extends AbstractConverter<String, URI> {
 82  
         public StringToURI() {
 83  2
             super(String.class, URI.class);
 84  2
         }
 85  
 
 86  
         public URI convert(String obj) throws ConversionException {
 87  
             try {
 88  1
                 return new URI(obj);
 89  0
             } catch (URISyntaxException e) {
 90  0
                 throw (ConversionException) new ConversionException(e.getMessage()).initCause(e);
 91  
             }
 92  
         }
 93  
     }
 94  
 
 95  
     /**
 96  
      * An object that converts a <code>String</code> to a
 97  
      * <code>URL</code>.
 98  
      */
 99  1
     public static class StringToURL extends AbstractConverter<String, URL> {
 100  
         public StringToURL() {
 101  2
             super(String.class, URL.class);
 102  2
         }
 103  
 
 104  
         public URL convert(String obj) throws ConversionException {
 105  
             try {
 106  1
                 return new URL(obj);
 107  0
             } catch (MalformedURLException e) {
 108  0
                 throw (ConversionException) new ConversionException(e.getMessage()).initCause(e);
 109  
             }
 110  
         }
 111  
     }
 112  
 
 113  
     /**
 114  
      * An object that converts a <code>URI</code> to a
 115  
      * <code>URL</code>.
 116  
      */
 117  1
     public static class URIToURL extends AbstractConverter<URI, URL> {
 118  
         public URIToURL() {
 119  3
             super(URI.class, URL.class);
 120  3
         }
 121  
 
 122  
         public URL convert(URI obj) throws ConversionException {
 123  
             try {
 124  1
                 return obj.toURL();
 125  0
             } catch (MalformedURLException e) {
 126  0
                 throw (ConversionException) new ConversionException(e.getMessage()).initCause(e);
 127  
             }
 128  
         }
 129  
     }
 130  
 
 131  
     /**
 132  
      * An object that converts a <code>URL</code> to a
 133  
      * <code>URI</code>.
 134  
      */
 135  1
     public static class URLToURI extends AbstractConverter<URL, URI> {
 136  
         public URLToURI() {
 137  2
             super(URL.class, URI.class);
 138  2
         }
 139  
 
 140  
         public URI convert(URL obj) throws ConversionException {
 141  
             try {
 142  1
                 return obj.toURI();
 143  0
             } catch (URISyntaxException e) {
 144  0
                 throw (ConversionException) new ConversionException(e.getMessage()).initCause(e);
 145  
             }
 146  
         }
 147  
     }
 148  
 }