EntandoCustomResourceReferenceFluent.java

/*
 *
 * Copyright 2015-Present Entando Inc. (http://www.entando.com) All rights reserved.
 *
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2.1 of the License, or (at your option)
 * any later version.
 *
 *  This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 *
 */

package org.entando.kubernetes.model.compositeapp;

import io.fabric8.kubernetes.api.builder.Nested;
import io.fabric8.kubernetes.api.model.ObjectMeta;
import io.fabric8.kubernetes.api.model.ObjectMetaBuilder;
import org.entando.kubernetes.model.common.EntandoFluent;

public abstract class EntandoCustomResourceReferenceFluent<N extends EntandoCustomResourceReferenceFluent<N>> extends
        EntandoFluent<N> {

    protected EntandoCustomResourceReferenceSpecBuilder spec;

    protected EntandoCustomResourceReferenceFluent() {
        this(new ObjectMetaBuilder(), new EntandoCustomResourceReferenceSpecBuilder());
    }

    protected EntandoCustomResourceReferenceFluent(EntandoCustomResourceReferenceSpec spec, ObjectMeta objectMeta) {
        this(new ObjectMetaBuilder(objectMeta), new EntandoCustomResourceReferenceSpecBuilder(spec));
    }

    private EntandoCustomResourceReferenceFluent(ObjectMetaBuilder metadata, EntandoCustomResourceReferenceSpecBuilder spec) {
        super(metadata);
        this.spec = spec;
    }

    public NestedEntandoCustomResourceReferenceSpecFluent<N> editSpec() {
        return new NestedEntandoCustomResourceReferenceSpecFluent<>(thisAsA(), this.spec.build());
    }

    public NestedEntandoCustomResourceReferenceSpecFluent<N> withNewSpec() {
        return new NestedEntandoCustomResourceReferenceSpecFluent<>(thisAsA());
    }

    public N withSpec(EntandoCustomResourceReferenceSpec spec) {
        this.spec = new EntandoCustomResourceReferenceSpecBuilder(spec);
        return thisAsA();
    }

    @SuppressWarnings("unchecked")
    protected N thisAsA() {
        return (N) this;
    }

    public static class NestedEntandoCustomResourceReferenceSpecFluent<P extends EntandoCustomResourceReferenceFluent<P>> extends
            EntandoCustomResourceReferenceSpecFluent<NestedEntandoCustomResourceReferenceSpecFluent<P>> implements
            Nested<P> {

        private final P parentBuilder;

        NestedEntandoCustomResourceReferenceSpecFluent(P parentBuilder, EntandoCustomResourceReferenceSpec spec) {
            super(spec);
            this.parentBuilder = parentBuilder;
        }

        NestedEntandoCustomResourceReferenceSpecFluent(P parentBuilder) {
            super();
            this.parentBuilder = parentBuilder;
        }

        @Override
        public P and() {
            return parentBuilder.withSpec(this.build());
        }

        public P endSpec() {
            return this.and();
        }
    }
}