001/* 002 * Java Genetic Algorithm Library (jenetics-7.2.0). 003 * Copyright (c) 2007-2023 Franz Wilhelmstötter 004 * 005 * Licensed under the Apache License, Version 2.0 (the "License"); 006 * you may not use this file except in compliance with the License. 007 * You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 * 017 * Author: 018 * Franz Wilhelmstötter (franz.wilhelmstoetter@gmail.com) 019 */ 020package io.jenetics.internal.collection; 021 022import static java.util.Objects.requireNonNull; 023 024import java.util.Collections; 025import java.util.Comparator; 026import java.util.Iterator; 027import java.util.List; 028import java.util.ListIterator; 029import java.util.Spliterator; 030import java.util.Spliterators; 031import java.util.function.Function; 032import java.util.stream.Stream; 033 034import io.jenetics.util.ISeq; 035import io.jenetics.util.MSeq; 036 037/** 038 * Contains static {@code Seq} definitions. 039 * 040 * @author <a href="mailto:franz.wilhelmstoetter@gmail.com">Franz Wilhelmstötter</a> 041 * @version 4.1 042 * @since 3.4 043 */ 044public final class Empty { 045 private Empty() {} 046 047 public enum EmptyMSeq implements MSeq<Object> { 048 049 INSTANCE; 050 051 @Override 052 public void set(final int index, final Object value) { 053 throw new ArrayIndexOutOfBoundsException("MSeq is empty."); 054 } 055 056 @Override 057 public MSeq<Object> sort( 058 final int start, 059 final int end, 060 final Comparator<? super Object> comparator 061 ) { 062 Array.checkIndex(start, end, length()); 063 return this; 064 } 065 066 @Override 067 public ListIterator<Object> listIterator() { 068 return Collections.emptyListIterator(); 069 } 070 071 @Override 072 public MSeq<Object> subSeq(final int start, final int end) { 073 Array.checkIndex(start, end, length()); 074 return this; 075 } 076 077 @Override 078 public MSeq<Object> subSeq(final int start) { 079 Array.checkIndex(start, 0, length()); 080 return this; 081 } 082 083 @Override 084 public <B> MSeq<B> map(final Function<? super Object, ? extends B> mapper) { 085 requireNonNull(mapper); 086 return mseq(); 087 } 088 089 @Override 090 public MSeq<Object> append(final Object... values) { 091 return MSeq.of(values); 092 } 093 094 @Override 095 public MSeq<Object> append(final Iterable<?> values) { 096 return MSeq.of(values); 097 } 098 099 @Override 100 public MSeq<Object> prepend(final Object... values) { 101 return MSeq.of(values); 102 } 103 104 @Override 105 public MSeq<Object> prepend(final Iterable<?> values) { 106 return MSeq.of(values); 107 } 108 109 @Override 110 public Stream<Object> stream() { 111 return Stream.empty(); 112 } 113 114 @Override 115 public Stream<Object> parallelStream() { 116 return Stream.empty(); 117 } 118 119 @Override 120 public Spliterator<Object> spliterator() { 121 return Spliterators.emptySpliterator(); 122 } 123 124 @Override 125 public ISeq<Object> toISeq() { 126 return EmptyISeq.INSTANCE; 127 } 128 129 @Override 130 public MSeq<Object> copy() { 131 return this; 132 } 133 134 @Override 135 public Object get(final int index) { 136 throw new ArrayIndexOutOfBoundsException("MSeq is empty: " + index); 137 } 138 139 @Override 140 public int length() { 141 return 0; 142 } 143 144 @Override 145 public List<Object> asList() { 146 return Collections.emptyList(); 147 } 148 149 @Override 150 public Iterator<Object> iterator() { 151 return Collections.emptyIterator(); 152 } 153 154 @Override 155 public String toString() { 156 return "[]"; 157 } 158 159 } 160 161 public enum EmptyISeq implements ISeq<Object> { 162 163 INSTANCE; 164 165 @Override 166 public Iterator<Object> iterator() { 167 return Collections.emptyIterator(); 168 } 169 170 @Override 171 public ISeq<Object> subSeq(final int start, final int end) { 172 Array.checkIndex(start, end, length()); 173 return this; 174 } 175 176 @Override 177 public ISeq<Object> subSeq(final int start) { 178 Array.checkIndex(start, 0, length()); 179 return this; 180 } 181 182 @Override 183 public Object get(final int index) { 184 throw new ArrayIndexOutOfBoundsException("ISeq is empty: " + index); 185 } 186 187 @Override 188 public int length() { 189 return 0; 190 } 191 192 @Override 193 public List<Object> asList() { 194 return Collections.emptyList(); 195 } 196 197 @Override 198 public <B> ISeq<B> map(final Function<? super Object, ? extends B> mapper) { 199 requireNonNull(mapper); 200 return iseq(); 201 } 202 203 @Override 204 public ISeq<Object> append(final Object... values) { 205 return ISeq.of(values); 206 } 207 208 @Override 209 public ISeq<Object> append(final Iterable<?> values) { 210 return ISeq.of(values); 211 } 212 213 @Override 214 public ISeq<Object> prepend(final Object... values) { 215 return ISeq.of(values); 216 } 217 218 @Override 219 public ISeq<Object> prepend(final Iterable<?> values) { 220 return ISeq.of(values); 221 } 222 223 @Override 224 public Stream<Object> stream() { 225 return Stream.empty(); 226 } 227 228 @Override 229 public Stream<Object> parallelStream() { 230 return Stream.empty(); 231 } 232 233 @Override 234 public Spliterator<Object> spliterator() { 235 return Spliterators.emptySpliterator(); 236 } 237 238 @Override 239 public MSeq<Object> copy() { 240 return EmptyMSeq.INSTANCE; 241 } 242 243 @Override 244 public String toString() { 245 return "[]"; 246 } 247 248 } 249 250 @SuppressWarnings("unchecked") 251 public static <T> MSeq<T> mseq() { 252 return (MSeq<T>)EmptyMSeq.INSTANCE; 253 } 254 255 @SuppressWarnings("unchecked") 256 public static <T> ISeq<T> iseq() { 257 return (ISeq<T>)EmptyISeq.INSTANCE; 258 } 259 260}