All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.ykrasik.jaci.util.opt.Absent Maven / Gradle / Ivy

There is a newer version: 0.4.0
Show newest version
/******************************************************************************
 * Copyright (C) 2015 Yevgeny Krasik                                          *
 *                                                                            *
 * Licensed under the Apache License, Version 2.0 (the "License");            *
 * you may not use this file except in compliance with the License.           *
 * You may obtain a copy of the License at                                    *
 *                                                                            *
 * http://www.apache.org/licenses/LICENSE-2.0                                 *
 *                                                                            *
 * Unless required by applicable law or agreed to in writing, software        *
 * distributed under the License is distributed on an "AS IS" BASIS,          *
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   *
 * See the License for the specific language governing permissions and        *
 * limitations under the License.                                             *
 ******************************************************************************/

package com.github.ykrasik.jaci.util.opt;

import com.github.ykrasik.jaci.util.function.Func;
import com.github.ykrasik.jaci.util.function.Pred;

import java.util.*;

/**
 * An implementation of an {@code absent} {@link Opt}.
 *
 * @author Yevgeny Krasik
 */
@SuppressWarnings("unchecked")
final class Absent extends Opt {
    private static final long serialVersionUID = 0;
    private static final Absent INSTANCE = new Absent<>();

    private Absent() { }

    @Override
    public boolean isPresent() {
        return false;
    }

    @Override
    public T get() {
        throw new IllegalStateException("Called get() on an absent value!");
    }

    @Override
    public T getOrElseNull() {
        return null;
    }

    @Override
    public T getOrElse(T defaultValue) {
        return defaultValue;
    }

    @Override
    public Opt orElse(Opt alternative) {
        return (Opt) Objects.requireNonNull(alternative, "alternative");
    }

    @Override
    public  Opt map(Func function) {
        return instance();
    }

    @Override
    public  Opt flatMap(Func> function) {
        return instance();
    }

    @Override
    public boolean exists(Pred predicate) {
        return false;
    }

    @Override
    public Opt filter(Pred predicate) {
        return instance();
    }

    @Override
    public List toList() {
        return Collections.emptyList();
    }

    @Override
    public Set toSet() {
        return Collections.emptySet();
    }

    @Override
    public  Map toMap(K key) {
        return Collections.emptyMap();
    }

    @Override
    public boolean equals(Object object) {
        return object == this;
    }

    @Override
    public int hashCode() {
        return 0;
    }

    @Override
    public String toString() {
        return "Absent";
    }

    private Object readResolve() {
        return INSTANCE;
    }

    static  Opt instance() {
        return (Opt) INSTANCE;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy