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

com.github.rvesse.airline.model.ArgumentsMetadata Maven / Gradle / Ivy

Go to download

Java library provided an annotation-based framework for parsing Git like command line structures

The newest version!
/**
 * Copyright (C) 2010-16 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.github.rvesse.airline.model;

import com.github.rvesse.airline.Accessor;
import com.github.rvesse.airline.restrictions.ArgumentsRestriction;
import com.github.rvesse.airline.types.DefaultTypeConverterProvider;
import com.github.rvesse.airline.types.TypeConverterProvider;
import com.github.rvesse.airline.utils.AirlineUtils;
import com.github.rvesse.airline.utils.predicates.restrictions.IsRequiredArgumentFinder;

import java.lang.reflect.Field;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.IteratorUtils;
import org.apache.commons.collections4.ListUtils;
import org.apache.commons.collections4.SetUtils;
import org.apache.commons.lang3.StringUtils;

public class ArgumentsMetadata {
    private final List titles;
    private final String description;
    private final Set accessors;
    private final List restrictions;
    private final TypeConverterProvider provider;

    //@formatter:off
    public ArgumentsMetadata(Iterable titles, 
                             String description, 
                             Iterable restrictions, 
                             TypeConverterProvider typeConverterProvider,
                             Iterable path) {
    //@formatter:on
        if (titles == null)
            throw new NullPointerException("title cannot be null");
        if (path == null)
            throw new NullPointerException("path cannot be null");
        if (!path.iterator().hasNext())
            throw new IllegalArgumentException("path cannot be empty");

        this.titles = ListUtils.unmodifiableList(IteratorUtils.toList(titles.iterator()));
        this.description = description;
        this.restrictions = restrictions != null ? AirlineUtils.unmodifiableListCopy(restrictions)
                : Collections. emptyList();
        this.provider = typeConverterProvider != null ? typeConverterProvider : new DefaultTypeConverterProvider();
        this.accessors = SetUtils.unmodifiableSet(Collections.singleton(new Accessor(path)));
    }

    public ArgumentsMetadata(Iterable arguments) {
        if (arguments == null)
            throw new NullPointerException("arguments cannot be null");
        if (!arguments.iterator().hasNext())
            throw new IllegalArgumentException("arguments cannot be empty");

        ArgumentsMetadata first = arguments.iterator().next();

        this.titles = first.titles;
        this.description = first.description;
        this.restrictions = first.restrictions;
        this.provider = first.provider;

        Set accessors = new HashSet<>();
        for (ArgumentsMetadata other : arguments) {
            if (!first.equals(other))
                throw new IllegalArgumentException(
                        String.format("Conflicting arguments definitions: %s, %s", first, other));

            accessors.addAll(other.getAccessors());
        }
        this.accessors = SetUtils.unmodifiableSet(accessors);
    }

    public List getTitle() {
        return titles;
    }

    public String getDescription() {
        return description;
    }

    public boolean isRequired() {
        return CollectionUtils.exists(this.restrictions, new IsRequiredArgumentFinder());
    }

    public Set getAccessors() {
        return accessors;
    }

    public boolean isMultiValued() {
        return accessors.iterator().next().isMultiValued();
    }

    public Class getJavaType() {
        return accessors.iterator().next().getJavaType();
    }

    public List getRestrictions() {
        return this.restrictions;
    }
    
    public TypeConverterProvider getTypeConverterProvider() {
        return this.provider;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }

        ArgumentsMetadata that = (ArgumentsMetadata) o;

        if (description != null ? !description.equals(that.description) : that.description != null) {
            return false;
        }
        if (!titles.equals(that.titles)) {
            return false;
        }

        return true;
    }

    @Override
    public int hashCode() {
        int result = titles.hashCode();
        result = 31 * result + (description != null ? description.hashCode() : 0);
        return result;
    }

    @Override
    public String toString() {
        final StringBuilder sb = new StringBuilder();
        sb.append("ArgumentsMetadata");
        sb.append("{title='").append(StringUtils.join(titles, ',')).append('\'');
        sb.append(", description='").append(description).append('\'');
        sb.append(", accessors=").append(accessors);
        sb.append('}');
        return sb.toString();
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy