Coverage Report - eu.simuline.octave.io.spi.OctaveDataReader
 
Classes in this File Line Coverage Branch Coverage Complexity
OctaveDataReader
78%
15/19
70%
7/10
2.25
 
 1  
 /*
 2  
  * Copyright 2008 Ange Optimization ApS
 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  
  * @author Kim Hansen
 18  
  */
 19  
 package eu.simuline.octave.io.spi;
 20  
 
 21  
 import java.io.BufferedReader;
 22  
 import java.util.HashMap;
 23  
 import java.util.Iterator;
 24  
 import java.util.Map;
 25  
 
 26  
 import javax.imageio.spi.ServiceRegistry;
 27  
 
 28  
 import eu.simuline.octave.type.OctaveObject;
 29  
 
 30  
 /**
 31  
  * Service Provider Interface for the IO handler 
 32  
  * that can read {@link OctaveObject}s. 
 33  
  * The octave type which can be read is given by {@link #octaveType()} 
 34  
  * whereas {@link #read(BufferedReader)} performs reading. 
 35  
  * <p>
 36  
  * The according implementations 
 37  
  * are in package {@link eu.simuline.octave.io.impl} 
 38  
  * and extend this class. 
 39  
  * These classes are registered in the jar-file 
 40  
  * under <code>META-INF/services/eu.simuline.octave.io.OctaveDataReader</code>. 
 41  
  */
 42  38
 public abstract class OctaveDataReader {
 43  
 
 44  
     /**
 45  
      * Maps the {@link #octaveType()} 
 46  
      * of an {@link OctaveDataReader} to the {@link OctaveDataReader} itself 
 47  
      * which is able to read the octave type from a reader. 
 48  
      */
 49  2
     private static Map<String, OctaveDataReader> rEADERS = null;
 50  
 
 51  
     /**
 52  
      * @param type
 53  
      * @return The OctaveDataReader or null if it does not exist
 54  
      */
 55  
     public static OctaveDataReader getOctaveDataReader(final String type) {
 56  752
         initReaderIfNecessary();
 57  752
         return rEADERS.get(type);
 58  
     }
 59  
 
 60  
     private static synchronized void initReaderIfNecessary() {
 61  752
         if (rEADERS != null) {
 62  750
             return;
 63  
         }
 64  2
         rEADERS = new HashMap<String, OctaveDataReader>();
 65  2
         final Iterator<OctaveDataReader> sp = 
 66  2
             ServiceRegistry.lookupProviders(OctaveDataReader.class);
 67  
         OctaveDataReader odr, odrOrg;
 68  36
         while (sp.hasNext()) {
 69  34
             odr = sp.next();
 70  34
             assert odr != null;
 71  34
             odrOrg = rEADERS.put(odr.octaveType(), odr);
 72  34
             if (odrOrg != null) {
 73  0
                  throw new IllegalStateException
 74  0
                      ("Octave type " + odr.octaveType() + 
 75  0
                       " has readers of type " + odr.getClass() + 
 76  0
                       " and " + odrOrg.getClass() + ". ");
 77  
             }
 78  
         }
 79  2
     }
 80  
 
 81  
     /**
 82  
      * Could be "scalar" or "string" or something else. 
 83  
      *
 84  
      * @return
 85  
      *    the string representation of the octave type 
 86  
      *    read by this {@link OctaveDataReader} 
 87  
      */
 88  
     public abstract String octaveType();
 89  
 
 90  
     /**
 91  
      * Reads an {@link OctaveObject} from a Reader <code>reader</code>. 
 92  
      * @param reader
 93  
      *    the Reader to read from, will not close reader
 94  
      * @return
 95  
      *   the object read from <code>reader</code>. 
 96  
      */
 97  
     public abstract OctaveObject read(BufferedReader reader);
 98  
 
 99  
 }