View Javadoc
1   /*
2    * This file is part of Waarp Project (named also Waarp or GG).
3    *
4    *  Copyright (c) 2019, Waarp SAS, and individual contributors by the @author
5    *  tags. See the COPYRIGHT.txt in the distribution for a full listing of
6    * individual contributors.
7    *
8    *  All Waarp Project is free software: you can redistribute it and/or
9    * modify it under the terms of the GNU General Public License as published by
10   * the Free Software Foundation, either version 3 of the License, or (at your
11   * option) any later version.
12   *
13   * Waarp is distributed in the hope that it will be useful, but WITHOUT ANY
14   * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
15   * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16   *
17   *  You should have received a copy of the GNU General Public License along with
18   * Waarp . If not, see <http://www.gnu.org/licenses/>.
19   */
20  package org.waarp.common.utility;
21  
22  import java.io.InputStream;
23  import java.io.OutputStream;
24  import java.util.Collections;
25  import java.util.List;
26  import java.util.Map;
27  import java.util.Set;
28  
29  /**
30   * Singleton utility class
31   */
32  public final class SingletonUtils {
33    private static final byte[] SINGLETON_BYTE_ARRAY = {};
34    private static final InputStream SINGLETON_INPUTSTREAM =
35        new NullInputStream();
36    private static final OutputStream SINGLETON_OUTPUTSTREAM =
37        new VoidOutputStream();
38  
39    private SingletonUtils() {
40      // empty
41    }
42  
43  
44    /**
45     * Immutable empty byte array
46     *
47     * @return a Byte Array Singleton
48     */
49    public static byte[] getSingletonByteArray() {
50      return SINGLETON_BYTE_ARRAY;
51    }
52  
53    /**
54     * Immutable empty List
55     *
56     * @return an immutable empty List
57     */
58    public static <E> List<E> singletonList() {
59      return Collections.emptyList();
60    }
61  
62    /**
63     * Immutable empty Set
64     *
65     * @return an immutable empty Set
66     */
67    public static <E> Set<E> singletonSet() {
68      return Collections.emptySet();
69    }
70  
71    /**
72     * Immutable empty Map
73     *
74     * @return an immutable empty Map
75     */
76    public static <E, V> Map<E, V> singletonMap() {
77      return Collections.emptyMap();
78    }
79  
80    /**
81     * Empty InputStream
82     */
83    private static final class NullInputStream extends InputStream {
84      @Override
85      public final int read() {
86        return -1;
87      }
88  
89      @Override
90      public final int available() {
91        return 0;
92      }
93  
94      @Override
95      public final void close() {
96        // Empty
97      }
98  
99      @Override
100     public final void mark(final int arg0) {//NOSONAR
101       // Empty
102     }
103 
104     @Override
105     public final boolean markSupported() {
106       return true;
107     }
108 
109     @Override
110     public final int read(final byte[] arg0, final int arg1, final int arg2) {
111       return -1;
112     }
113 
114     @Override
115     public final int read(final byte[] arg0) {
116       return -1;
117     }
118 
119     @Override
120     public final void reset() {//NOSONAR
121       // Empty
122     }
123 
124     @Override
125     public final long skip(final long arg0) {
126       return 0;
127     }
128   }
129 
130   /**
131    * Immutable empty InputStream
132    *
133    * @return an immutable empty InputStream
134    */
135   public static InputStream singletonInputStream() {
136     return SINGLETON_INPUTSTREAM;
137   }
138 
139   /**
140    * OutputStream discarding all writed elements
141    */
142   private static final class VoidOutputStream extends OutputStream {
143     @Override
144     public final void close() {
145       // Empty
146     }
147 
148     @Override
149     public final void flush() {
150       // Empty
151     }
152 
153     @Override
154     public final void write(final byte[] arg0, final int arg1, final int arg2) {
155       // Empty
156     }
157 
158     @Override
159     public final void write(final byte[] arg0) {
160       // Empty
161     }
162 
163     @Override
164     public final void write(final int arg0) {
165       // Empty
166     }
167   }
168 
169   /**
170    * Immutable void OutputStream. Any write elements are discarder (equivalent
171    * to /dev/null).
172    *
173    * @return an immutable empty OutputStream
174    */
175   public static OutputStream singletonOutputStream() {
176     return SINGLETON_OUTPUTSTREAM;
177   }
178 
179 }