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

com.github.jonathanxd.iutils.object.Reference 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.annotations.NotNull;

import java.util.Arrays;
import java.util.Objects;
import java.util.StringJoiner;

/**
 * Created by jonathan on 13/02/16.
 */
public class Reference implements Comparable {

    private final Class aClass;
    private final Reference[] related;
    private final Object hold;

    Reference(Class aClass, Reference[] related, Object hold) {
        this.hold = hold;
        this.aClass = Objects.requireNonNull(aClass);
        this.related = related != null ? related : new Reference[0];
    }

    @NotNull
    public static String toString(Reference reference) {

        StringBuilder sb = new StringBuilder();
        String shortName = reference.getAClass().getSimpleName();
        sb.append(shortName);

        if (reference.getRelated().length != 0) {
            sb.append("<");
            StringJoiner sj = new StringJoiner(", ");

            for (Reference loopRef : reference.getRelated()) {
                sj.add(toString(loopRef));
            }

            String processResult = sj.toString();
            sb.append(processResult);
            sb.append(">");
        }

        return sb.toString();
    }

    public static  ReferenceBuilder to() {
        return referenceTo();
    }

    public static  ReferenceBuilder referenceTo() {
        return new ReferenceBuilder<>();
    }

    public static  ReferenceBuilder a(Class aClass) {
        return Reference.referenceTo().a(aClass);
    }

    public static  Reference aEnd(Class aClass) {
        return Reference.referenceTo().a(aClass).build();
    }

    @SuppressWarnings("unchecked")
    public static  ReferenceBuilder but(Reference reference) {
        return Reference.referenceTo().a(reference.getAClass()).of(reference.getRelated());
    }

    public ReferenceBuilder but() {
        return Reference.but(this);
    }

    public Class getAClass() {
        return aClass;
    }

    public Object get() {
        return hold;
    }

    public Reference[] getRelated() {
        return related;
    }

    @Override
    public String toString() {
        return toString(this);
    }

    @Override
    public int hashCode() {
        return Objects.hash(aClass, Arrays.deepHashCode(related));
    }

    @Override
    public boolean equals(Object obj) {

        if (!(obj instanceof Reference))
            return false;

        Reference other = (Reference) obj;

        return compareTo(other) == 0;
    }

    @Override
    public int compareTo(@NotNull Reference compareTo) {

        if (getAClass() == compareTo.getAClass()) {

            if (Arrays.deepEquals(getRelated(), compareTo.getRelated())) {
                return 0;
            }

            return 1;
        }

        return -1;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy