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

com.facebook.presto.spi.resourceGroups.ResourceGroupId Maven / Gradle / Ivy

There is a newer version: 0.289
Show newest version
/*
 * 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.facebook.presto.spi.resourceGroups;

import com.facebook.drift.annotations.ThriftConstructor;
import com.facebook.drift.annotations.ThriftField;
import com.facebook.drift.annotations.ThriftStruct;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonValue;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Optional;

import static java.lang.String.format;
import static java.util.Collections.singletonList;
import static java.util.Objects.requireNonNull;
import static java.util.stream.Collectors.joining;

@ThriftStruct
public final class ResourceGroupId
{
    private final List segments;

    public ResourceGroupId(String name)
    {
        this(singletonList(requireNonNull(name, "name is null")));
    }

    public ResourceGroupId(ResourceGroupId parent, String name)
    {
        this(append(requireNonNull(parent, "parent is null").segments, requireNonNull(name, "name is null")));
    }

    private static List append(List list, String element)
    {
        List result = new ArrayList<>(list);
        result.add(element);
        return result;
    }

    @ThriftConstructor
    @JsonCreator
    public ResourceGroupId(List segments)
    {
        checkArgument(!segments.isEmpty(), "Resource group id is empty");
        for (String segment : segments) {
            checkArgument(!segment.isEmpty(), "Empty segment in resource group id");
        }
        this.segments = segments;
    }

    public String getLastSegment()
    {
        return segments.get(segments.size() - 1);
    }

    @ThriftField(1)
    @JsonValue
    public List getSegments()
    {
        return segments;
    }

    public ResourceGroupId getRoot()
    {
        return new ResourceGroupId(segments.get(0));
    }

    public Optional getParent()
    {
        if (segments.size() == 1) {
            return Optional.empty();
        }
        return Optional.of(new ResourceGroupId(segments.subList(0, segments.size() - 1)));
    }

    public boolean isAncestorOf(ResourceGroupId descendant)
    {
        List descendantSegments = descendant.getSegments();
        if (segments.size() >= descendantSegments.size()) {
            return false;
        }
        return descendantSegments.subList(0, segments.size()).equals(segments);
    }

    private static void checkArgument(boolean argument, String format, Object... args)
    {
        if (!argument) {
            throw new IllegalArgumentException(format(format, args));
        }
    }

    @Override
    public String toString()
    {
        return segments.stream()
                .collect(joining("."));
    }

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

    @Override
    public int hashCode()
    {
        return Objects.hash(segments);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy