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

org.nohope.jaxb2.plugin.metadata.Descriptor Maven / Gradle / Ivy

There is a newer version: 0.2.3
Show newest version
package org.nohope.jaxb2.plugin.metadata;

import org.nohope.reflection.TypeReference;

import javax.annotation.Nullable;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * @author Ketoth Xupack
 * @since 2013-10-30 16:17
 */
public class Descriptor implements IDescriptor {
    private final List> callChain = new ArrayList<>();
    private final IDescriptor parent;
    private final TypeReference type;
    private final String name;

    public Descriptor(@Nullable final Descriptor parent,
                      @Nullable final String name,
                      final TypeReference type) {
        this.parent = parent;
        this.type = type;
        this.name = name;

        if (parent != null) {
            callChain.addAll(parent.callChain);
            callChain.add(parent);
        }
    }

    @Override
    public String getName() {
        return name;
    }

    @Nullable
    @Override
    public IDescriptor getParent() {
        return parent;
    }

    @Override
    public TypeReference getType() {
        return type;
    }

    @Override
    public final Iterator> iterator() {
        final List> callChain = new ArrayList<>();
        callChain.addAll(this.callChain);
        callChain.add(callChain.size(), this);

        return callChain.iterator();
    }

    @Override
    public String toString() {
        final StringBuilder builder = new StringBuilder();
        for (final IDescriptor descriptor : this) {
            if (descriptor.getParent() == null) {
                builder.append(descriptor.getType().getTypeClass().getCanonicalName());
            } else {
                builder.append('#')
                       .append(descriptor.getName())
                       .append('[')
                       .append(descriptor.getType().getTypeClass().getCanonicalName())
                       .append(']')
                       ;
            }
        }

        return builder.toString();
    }

    @Override
    public IDescriptor getChild(final String name) {
        throw new IllegalArgumentException("No children found");
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy