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

io.airlift.discovery.server.Service Maven / Gradle / Ivy

There is a newer version: 1.37
Show newest version
/*
 * Copyright 2010 Proofpoint, Inc.
 *
 * 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 io.airlift.discovery.server;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.common.base.Preconditions;
import com.google.common.base.Predicate;
import com.google.common.collect.ImmutableMap;

import javax.annotation.concurrent.Immutable;
import java.util.Map;

@Immutable
public class Service
{
    private final Id id;
    private final Id nodeId;
    private final String type;
    private final String pool;
    private final String location;
    private final Map properties;

    @JsonCreator
    public Service(
            @JsonProperty("id") Id id,
            @JsonProperty("nodeId") Id nodeId,
            @JsonProperty("type") String type,
            @JsonProperty("pool") String pool,
            @JsonProperty("location") String location,
            @JsonProperty("properties") Map properties)
    {
        Preconditions.checkNotNull(id, "id is null");
        Preconditions.checkNotNull(type, "type is null");
        Preconditions.checkNotNull(pool, "pool is null");
        Preconditions.checkNotNull(location, "location is null");
        Preconditions.checkNotNull(properties, "properties is null");

        this.id = id;
        this.nodeId = nodeId;
        this.type = type;
        this.pool = pool;
        this.location = location;
        this.properties = ImmutableMap.copyOf(properties);
    }

    @JsonProperty
    public Id getId()
    {
        return id;
    }

    @JsonProperty
    public Id getNodeId()
    {
        return nodeId;
    }

    @JsonProperty
    public String getType()
    {
        return type;
    }

    @JsonProperty
    public String getPool()
    {
        return pool;
    }

    @JsonProperty
    public String getLocation()
    {
        return location;
    }

    @JsonProperty
    public Map getProperties()
    {
        return properties;
    }

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

        Service that = (Service) o;

        if (!id.equals(that.id)) {
            return false;
        }

        return true;
    }

    @Override
    public int hashCode()
    {
        return id.hashCode();
    }


    public static Predicate matchesType(final String type)
    {
        return new Predicate()
        {
            public boolean apply(Service descriptor)
            {
                return descriptor.getType().equals(type);
            }
        };
    }

    public static Predicate matchesPool(final String pool)
    {
        return new Predicate()
        {
            public boolean apply(Service descriptor)
            {
                return descriptor.getPool().equals(pool);
            }
        };
    }

    @Override
    public String toString()
    {
        return "Service{" +
                "id=" + id +
                ", nodeId=" + nodeId +
                ", type='" + type + '\'' +
                ", pool='" + pool + '\'' +
                ", location='" + location + '\'' +
                ", properties=" + properties +
                '}';
    }

    public static Builder copyOf(StaticAnnouncement announcement)
    {
        return new Builder().copyOf(announcement);
    }

    public static class Builder
    {
        private Id id;
        private String type;
        private String pool;
        private String location;
        private Map properties;

        public Builder copyOf(StaticAnnouncement announcement)
        {
            type = announcement.getType();
            pool = announcement.getPool();
            properties = ImmutableMap.copyOf(announcement.getProperties());

            return this;
        }

        public Builder setId(Id id)
        {
            this.id = id;
            return this;
        }

        public Builder setLocation(String location)
        {
            this.location = location;
            return this;
        }

        public Service build()
        {
            // TODO: validate state
            return new Service(id, null, type, pool, location, properties);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy