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.internals;
020
021import org.nuxeo.lib.stream.log.LogPartition;
022import org.nuxeo.lib.stream.log.Name;
023
024/**
025 * @since 9.3
026 */
027public class LogPartitionGroup {
028    public final Name group;
029
030    public final Name name;
031
032    public final int partition;
033
034    public LogPartitionGroup(Name group, LogPartition mqp) {
035        this.group = group;
036        this.name = mqp.name();
037        this.partition = mqp.partition();
038    }
039
040    public LogPartitionGroup(Name group, Name name, int partition) {
041        this.group = group;
042        this.name = name;
043        this.partition = partition;
044    }
045
046    public LogPartition getLogPartition() {
047        return LogPartition.of(name, partition);
048    }
049
050    @Override
051    public boolean equals(Object o) {
052        if (this == o)
053            return true;
054        if (o == null || getClass() != o.getClass())
055            return false;
056        LogPartitionGroup that = (LogPartitionGroup) o;
057        return partition == that.partition && (group != null ? group.equals(that.group) : that.group == null)
058                && (name != null ? name.equals(that.name) : that.name == null);
059    }
060
061    @Override
062    public int hashCode() {
063        int result = group != null ? group.hashCode() : 0;
064        result = 31 * result + (name != null ? name.hashCode() : 0);
065        result = 31 * result + partition;
066        return result;
067    }
068
069    @Override
070    public String toString() {
071        return String.format("%s:%s-%02d", group.getUrn(), name.getUrn(), partition);
072    }
073
074}