001/*
002 * (C) Copyright 2017 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     bdelbosc
018 */
019package org.nuxeo.lib.stream.log;
020
021/**
022 * A tuple to store a Log name and the partition index.
023 *
024 * @since 9.3
025 */
026public class LogPartition {
027    protected final String name;
028
029    protected final int partition;
030
031    public LogPartition(String name, int partition) {
032        this.name = name;
033        this.partition = partition;
034    }
035
036    public static LogPartition of(String name, int partition) {
037        return new LogPartition(name, partition);
038    }
039
040    /**
041     * Returns the Log's name
042     */
043    public String name() {
044        return name;
045    }
046
047    /**
048     * Returns the partition index.
049     */
050    public int partition() {
051        return partition;
052    }
053
054    @Override
055    public boolean equals(Object o) {
056        if (this == o)
057            return true;
058        if (o == null || getClass() != o.getClass())
059            return false;
060
061        LogPartition that = (LogPartition) o;
062
063        return partition == that.partition && (name != null ? name.equals(that.name) : that.name == null);
064    }
065
066    @Override
067    public int hashCode() {
068        int result = name != null ? name.hashCode() : 0;
069        result = 31 * result + partition;
070        return result;
071    }
072
073    @Override
074    public String toString() {
075        return String.format("%s-%02d", name, partition);
076    }
077}