com.github.jonathanxd.iutils.object.ReferenceBuilder Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of textlexer Show documentation
Show all versions of textlexer Show documentation
Lexical Analyzer API for Java!
/*
* 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 extends T> aClass;
private List related = new ArrayList<>();
private Object hold = null;
ReferenceBuilder() {
}
@Required
public ReferenceBuilder a(Class extends T> 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 extends E>... classes) {
List> references = new ArrayList<>();
for (Class extends E> 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);
}
}