Skip to content

Commit

Permalink
Refactor OperationId and EventType to interfaces to support enums.
Browse files Browse the repository at this point in the history
  • Loading branch information
kuujo committed Jun 28, 2017
1 parent 791b3a3 commit 928363f
Show file tree
Hide file tree
Showing 14 changed files with 212 additions and 123 deletions.
4 changes: 2 additions & 2 deletions core/src/main/java/io/atomix/cluster/NodeId.java
Expand Up @@ -15,12 +15,12 @@
*/
package io.atomix.cluster;

import io.atomix.utils.Identifier;
import io.atomix.utils.AbstractIdentifier;

/**
* Controller cluster identity.
*/
public final class NodeId extends Identifier<String> implements Comparable<NodeId> {
public final class NodeId extends AbstractIdentifier<String> implements Comparable<NodeId> {

/**
* Constructor for serialization.
Expand Down
4 changes: 2 additions & 2 deletions core/src/main/java/io/atomix/cluster/PartitionId.java
Expand Up @@ -16,12 +16,12 @@
package io.atomix.cluster;

import com.google.common.base.Preconditions;
import io.atomix.utils.Identifier;
import io.atomix.utils.AbstractIdentifier;

/**
* {@link Partition} identifier.
*/
public class PartitionId extends Identifier<Integer> implements Comparable<PartitionId> {
public class PartitionId extends AbstractIdentifier<Integer> implements Comparable<PartitionId> {

/**
* Creates a partition identifier from an integer.
Expand Down
4 changes: 2 additions & 2 deletions core/src/main/java/io/atomix/transaction/TransactionId.java
Expand Up @@ -15,12 +15,12 @@
*/
package io.atomix.transaction;

import io.atomix.utils.Identifier;
import io.atomix.utils.AbstractIdentifier;

/**
* Transaction identifier.
*/
public final class TransactionId extends Identifier<String> {
public final class TransactionId extends AbstractIdentifier<String> {

/**
* Creates a new transaction identifier.
Expand Down
Expand Up @@ -15,27 +15,21 @@
*/
package io.atomix.protocols.raft;

import io.atomix.protocols.raft.impl.DefaultEventType;
import io.atomix.utils.Identifier;

/**
* Raft event identifier.
*/
public class EventType extends Identifier<String> {
public interface EventType extends Identifier<String> {

/**
* Creates a new Raft event identifier.
*
* @param name the event name
* @return the event identifier
*/
public static EventType from(String name) {
return new EventType(name);
}

private EventType() {
}

private EventType(String value) {
super(value);
static EventType from(String name) {
return new DefaultEventType(name);
}
}
Expand Up @@ -15,23 +15,22 @@
*/
package io.atomix.protocols.raft;

import io.atomix.protocols.raft.impl.DefaultOperationId;
import io.atomix.utils.Identifier;

import static com.google.common.base.MoreObjects.toStringHelper;

/**
* Raft operation identifier.
*/
public class OperationId extends Identifier<String> {
public static final OperationId NOOP = new OperationId(OperationType.COMMAND);
public interface OperationId extends Identifier<String> {
OperationId NOOP = new DefaultOperationId(OperationType.COMMAND);

/**
* Returns a new command operation identifier.
*
* @param id the command identifier
* @return the operation identifier
*/
public static OperationId command(String id) {
static OperationId command(String id) {
return from(id, OperationType.COMMAND);
}

Expand All @@ -41,7 +40,7 @@ public static OperationId command(String id) {
* @param id the query identifier
* @return the operation identifier
*/
public static OperationId query(String id) {
static OperationId query(String id) {
return from(id, OperationType.QUERY);
}

Expand All @@ -52,39 +51,14 @@ public static OperationId query(String id) {
* @param type the operation type
* @return the operation identifier
*/
public static OperationId from(String id, OperationType type) {
return new OperationId(id, type);
}

private final OperationType type;

protected OperationId() {
this.type = null;
}

private OperationId(OperationType type) {
this.type = type;
}

protected OperationId(String id, OperationType type) {
super(id);
this.type = type;
static OperationId from(String id, OperationType type) {
return new DefaultOperationId(id, type);
}

/**
* Returns the operation type.
*
* @return the operation type
*/
public OperationType type() {
return type;
}

@Override
public String toString() {
return toStringHelper(this)
.add("id", id())
.add("type", type())
.toString();
}
OperationType type();
}
Expand Up @@ -15,12 +15,12 @@
*/
package io.atomix.protocols.raft.cluster;

import io.atomix.utils.Identifier;
import io.atomix.utils.AbstractIdentifier;

/**
* Raft member ID.
*/
public final class MemberId extends Identifier<String> implements Comparable<MemberId> {
public final class MemberId extends AbstractIdentifier<String> implements Comparable<MemberId> {

/**
* Constructor for serialization.
Expand Down
@@ -0,0 +1,31 @@
/*
* Copyright 2017-present Open Networking Laboratory
*
* 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.atomix.protocols.raft.impl;

import io.atomix.protocols.raft.EventType;
import io.atomix.utils.AbstractIdentifier;

/**
* Default Raft event identifier.
*/
public class DefaultEventType extends AbstractIdentifier<String> implements EventType {
private DefaultEventType() {
}

public DefaultEventType(String value) {
super(value);
}
}
@@ -0,0 +1,59 @@
/*
* Copyright 2017-present Open Networking Laboratory
*
* 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.atomix.protocols.raft.impl;

import io.atomix.protocols.raft.OperationId;
import io.atomix.protocols.raft.OperationType;
import io.atomix.utils.AbstractIdentifier;

import static com.google.common.base.MoreObjects.toStringHelper;

/**
* Raft operation identifier.
*/
public class DefaultOperationId extends AbstractIdentifier<String> implements OperationId {
private final OperationType type;

protected DefaultOperationId() {
this.type = null;
}

public DefaultOperationId(OperationType type) {
this.type = type;
}

public DefaultOperationId(String id, OperationType type) {
super(id);
this.type = type;
}

/**
* Returns the operation type.
*
* @return the operation type
*/
public OperationType type() {
return type;
}

@Override
public String toString() {
return toStringHelper(this)
.add("id", id())
.add("type", type())
.toString();
}
}
Expand Up @@ -15,12 +15,12 @@
*/
package io.atomix.protocols.raft.session;

import io.atomix.utils.Identifier;
import io.atomix.utils.AbstractIdentifier;

/**
* Session identifier.
*/
public class SessionId extends Identifier<Long> {
public class SessionId extends AbstractIdentifier<Long> {

/**
* Returns a new session ID from the given identifier.
Expand Down
Expand Up @@ -15,12 +15,12 @@
*/
package io.atomix.protocols.raft.storage.snapshot;

import io.atomix.utils.Identifier;
import io.atomix.utils.AbstractIdentifier;

/**
* Snapshot identifier.
*/
public class StateMachineId extends Identifier<Long> {
public class StateMachineId extends AbstractIdentifier<Long> {

/**
* Creates a snapshot ID from the given number.
Expand Down
Expand Up @@ -19,6 +19,7 @@
import io.atomix.protocols.raft.cluster.RaftClusterEvent;
import io.atomix.protocols.raft.cluster.RaftMember;
import io.atomix.protocols.raft.cluster.impl.DefaultRaftMember;
import io.atomix.protocols.raft.impl.DefaultOperationId;
import io.atomix.protocols.raft.protocol.TestRaftProtocolFactory;
import io.atomix.protocols.raft.proxy.RaftProxy;
import io.atomix.protocols.raft.session.RaftSession;
Expand Down Expand Up @@ -80,7 +81,7 @@ public class RaftTest extends ConcurrentTestCase {
.register(OpenSessionEntry.class)
.register(QueryEntry.class)
.register(RaftOperation.class)
.register(OperationId.class)
.register(DefaultOperationId.class)
.register(OperationType.class)
.register(ReadConsistency.class)
.register(ArrayList.class)
Expand Down
Expand Up @@ -22,6 +22,7 @@
import io.atomix.protocols.raft.cluster.RaftMember;
import io.atomix.protocols.raft.cluster.impl.DefaultRaftMember;
import io.atomix.protocols.raft.error.RaftError;
import io.atomix.protocols.raft.impl.DefaultOperationId;
import io.atomix.protocols.raft.protocol.AppendRequest;
import io.atomix.protocols.raft.protocol.AppendResponse;
import io.atomix.protocols.raft.protocol.CloseSessionRequest;
Expand Down Expand Up @@ -173,7 +174,7 @@ public static void main(String[] args) {
.register(OpenSessionEntry.class)
.register(QueryEntry.class)
.register(RaftOperation.class)
.register(OperationId.class)
.register(DefaultOperationId.class)
.register(OperationType.class)
.register(ReadConsistency.class)
.register(ArrayList.class)
Expand Down

0 comments on commit 928363f

Please sign in to comment.