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

com.github.jonathanxd.iutils.object.ReferenceBuilder Maven / Gradle / Ivy

There is a newer version: 1.7
Show newest version
/*
 * 	TextLexer - Lexical Analyzer API for Java! 
 *     Copyright (C) 2016 TheRealBuggy/JonathanxD (https://github.com/JonathanxD/) 
 *
 * 	GNU GPLv3
 *
 *     This program is free software: you can redistribute it and/or modify
 *     it under the terms of the GNU Affero General Public License as published
 *     by the Free Software Foundation.
 *
 *     This program 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 Affero General Public License for more details.
 *
 *     You should have received a copy of the GNU Affero General Public License
 *     along with this program.  If not, see .
 */
package com.github.jonathanxd.iutils.object;

import com.github.jonathanxd.iutils.optional.Optional;
import com.github.jonathanxd.iutils.optional.Required;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Created by jonathan on 13/02/16.
 */
public final class ReferenceBuilder {
    private Class aClass;
    private List related = new ArrayList<>();
    private Object hold = null;

    ReferenceBuilder() {
    }

    @Required
    public ReferenceBuilder a(Class aClass) {
        this.aClass = aClass;
        return this;
    }

    @Optional
    public ReferenceBuilder hold(Object object) {
        this.hold = object;
        return this;
    }

    // Of
    @Optional
    public  ReferenceBuilder of(List> related) {
        this.related.addAll(related);
        return this;
    }

    @SafeVarargs
    @Optional
    public final  ReferenceBuilder of(Reference... related) {
        of(Arrays.asList(related));
        return this;
    }

    @Optional
    public ReferenceBuilder of(ReferenceBuilder... builders) {

        for (ReferenceBuilder builder : builders) {
            of(builder.build());
        }
        return this;
    }

    @SafeVarargs
    @Optional
    public final  ReferenceBuilder of(Class... classes) {

        List> references = new ArrayList<>();

        for (Class classz : classes) {
            references.add(new ReferenceBuilder().a(classz).build());
        }

        of(references);

        return this;
    }

    // AND OF
    @Optional
    public  ReferenceBuilder and(List> related) {
        andCheck();
        of(related);
        return this;
    }

    @SafeVarargs
    @Optional
    public final  ReferenceBuilder and(Reference... related) {
        andCheck();
        of(related);
        return this;
    }

    @Optional
    public ReferenceBuilder and(ReferenceBuilder... builders) {
        andCheck();
        of(builders);
        return this;
    }

    @Optional
    public ReferenceBuilder and(Class... classes) {
        andCheck();
        of(classes);
        return this;
    }

    private void andCheck() {
        if (related.size() == 0)
            throw new IllegalStateException("'and' cannot be used here! Usage ex: referenceTo().a(Object.class).of(String.class).and(Class.class)");
    }

    public Reference build() {
        return new Reference<>(aClass, related.toArray(new Reference[related.size()]), hold);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy