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

org.gradle.internal.serialization.Transient Maven / Gradle / Ivy

There is a newer version: 8.6
Show newest version
/*
 * Copyright 2020 the original author or authors.
 *
 * 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 org.gradle.internal.serialization;

import javax.annotation.Nullable;

/**
 * A value that gets discarded during serialization.
 */
public abstract class Transient implements java.io.Serializable {

    /**
     * A mutable variable that gets discarded during serialization.
     */
    public static abstract class Var extends Transient {
        public abstract void set(T value);
    }

    public static  Transient of(T value) {
        return new ImmutableTransient(value);
    }

    public static  Var varOf() {
        return varOf(null);
    }

    public static  Var varOf(@Nullable T value) {
        return new MutableTransient(value);
    }

    @Nullable
    public abstract T get();

    private static class ImmutableTransient extends Transient {

        private final T value;

        public ImmutableTransient(T value) {
            this.value = value;
        }

        @Override
        public T get() {
            return value;
        }

        private Object writeReplace() {
            return DISCARDED;
        }
    }

    private static class MutableTransient extends Var {

        @Nullable
        private T value;

        public MutableTransient(@Nullable T value) {
            this.value = value;
        }

        @Override
        public T get() {
            return value;
        }

        @Override
        public void set(T value) {
            this.value = value;
        }

        private Object writeReplace() {
            return DISCARDED;
        }
    }

    private static class Discarded extends Var {

        @Override
        public void set(T value) {
            throw new IllegalStateException("The value of this property cannot be set after it has been discarded during serialization.");
        }

        @Override
        public T get() {
            throw new IllegalStateException("The value of this property has been discarded during serialization.");
        }

        private Object readResolve() {
            return DISCARDED;
        }
    }

    private static final Transient DISCARDED = new Discarded();
}