View Javadoc
1   package org.thegalactic.io;
2   
3   /*
4    * IOFactory.java
5    *
6    * Copyright: 2010-2015 Karell Bertet, France
7    * Copyright: 2015-2016 The Galactic Organization, France
8    *
9    * License: http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html CeCILL-B license
10   *
11   * This file is part of java-lattices.
12   * You can redistribute it and/or modify it under the terms of the CeCILL-B license.
13   */
14  import java.util.HashMap;
15  import java.util.Map;
16  
17  /**
18   * This class defines a standard way for getting reader and writer.
19   *
20   * @param <E> Element to be saved and parsed
21  
22   ![IOFactory](IOFactory.png)
23   *
24   * @uml Factory.png
25   * !include resources/org/thegalactic/io/IOFactory.iuml
26   * !include resources/org/thegalactic/io/Reader.iuml
27   * !include resources/org/thegalactic/io/Writer.iuml
28   *
29   * hide members
30   * show IOFactory members
31   * class IOFactory #LightCyan
32   * title IOFactory UML graph
33   */
34  public class IOFactory<E> {
35  
36      /**
37       * Map of extension/reader.
38       */
39      private final Map<String, Reader<E>> readers = new HashMap<String, Reader<E>>();
40  
41      /**
42       * Map of extension/writer.
43       */
44      private final Map<String, Writer<E>> writers = new HashMap<String, Writer<E>>();
45  
46      /**
47       * Basic constructor.
48       */
49      protected IOFactory() {
50          super();
51      }
52  
53      /**
54       * Register a reader with an extension.
55       *
56       * @param reader    The reader to register
57       * @param extension The extension linked to the reader
58       *
59       * @return The old reader or null
60       */
61      public final Reader<E> registerReader(final Reader<E> reader, final String extension) {
62          final Reader<E> old = this.readers.get(extension);
63          this.readers.put(extension, reader);
64          return old;
65      }
66  
67      /**
68       * Register a writer with an extension.
69       *
70       * @param writer    The writer to register
71       * @param extension The extension linked to the writer
72       *
73       * @return The old reader or null
74       */
75      public final Writer<E> registerWriter(final Writer<E> writer, final String extension) {
76          final Writer<E> old = this.writers.get(extension);
77          this.writers.put(extension, writer);
78          return old;
79      }
80  
81      /**
82       * Unregister a reader extension.
83       *
84       * @param extension The extension linked to a reader
85       *
86       * @return The old reader or null
87       */
88      public final Reader<E> unregisterReader(final String extension) {
89          final Reader<E> old = this.readers.get(extension);
90          this.readers.remove(extension);
91          return old;
92      }
93  
94      /**
95       * Unregister a writer extension.
96       *
97       * @param extension The extension linked to a writer
98       *
99       * @return The old writer or null
100      */
101     public final Writer<E> unregisterWriter(final String extension) {
102         final Writer<E> old = this.writers.get(extension);
103         this.writers.remove(extension);
104         return old;
105     }
106 
107     /**
108      * Get the reader linked to an extension.
109      *
110      * @param extension The extension linked to a reader
111      *
112      * @return The reader or null
113      */
114     public final Reader<E> getReader(final String extension) {
115         return this.readers.get(extension);
116     }
117 
118     /**
119      * Get the writer linked to an extension.
120      *
121      * @param extension The extension linked to a writer
122      *
123      * @return The writer or null
124      */
125     public final Writer<E> getWriter(final String extension) {
126         return this.writers.get(extension);
127     }
128 }