/*
 * Copyright 2015 the original author or authors.
 *
 * 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.copycat.resource;

import io.atomix.catalyst.serializer.SerializeWith;
import io.atomix.catalogue.client.Command;
import io.atomix.catalogue.client.Operation;
import io.atomix.catalyst.util.Assert;
import io.atomix.catalyst.util.BuilderPool;

/**
 * Resource command.
 *
 * @author <a href="http://github.com/kuujo">Jordan Halterman</a>
 */
@SerializeWith(id=400)
public class ResourceCommand<T extends Command<U>, U> extends ResourceOperation<T, U> implements Command<U> {

  /**
   * Returns a new resource command builder.
   *
   * @return A new resource command builder.
   */
  @SuppressWarnings("unchecked")
  public static <T extends Command<U>, U> Builder<T, U> builder() {
    return Operation.builder(Builder.class, Builder::new);
  }

  @Override
  public String toString() {
    return String.format("%s[resource=%d, command=%s]", getClass().getSimpleName(), resource, operation);
  }

  /**
   * Resource command builder.
   */
  public static class Builder<T extends Command<U>, U> extends Command.Builder<Builder<T, U>, ResourceCommand<T, U>, U> {

    private Builder(BuilderPool<Builder<T, U>, ResourceCommand<T, U>> pool) {
      super(pool);
    }

    @Override
    protected ResourceCommand<T, U> create() {
      return new ResourceCommand<>();
    }

    /**
     * Sets the resource ID.
     *
     * @param resource The resource ID.
     * @return The resource command builder.
     */
    public Builder withResource(long resource) {
      command.resource = resource;
      return this;
    }

    /**
     * Sets the resource command.
     *
     * @param command The resource command.
     * @return The resource command builder.
     * @throws NullPointerException if {@code command} is null
     */
    public Builder withCommand(T command) {
      this.command.operation = Assert.notNull(command, "command");
      return this;
    }
  }

}

@@ -645,60 +645,8 @@
 e;%0A%0A
-import io.atomix.catalyst.serializer.SerializeWith;%0A
 impo
@@ -729,16 +729,68 @@
 ration;%0A
+import io.atomix.catalyst.serializer.SerializeWith;%0A
 import i
@@ -1369,16 +1369,652 @@
 );%0A  %7D%0A%0A
+  @Override%0A  public ConsistencyLevel consistency() %7B%0A    return operation.consistency();%0A  %7D%0A%0A  @Override%0A  public PersistenceLevel persistence() %7B%0A    return operation.persistence();%0A  %7D%0A%0A  @Override%0A  public int groupCode() %7B%0A    return 37 * (int)(resource %5E (resource %3E%3E%3E 32)) + operation.groupCode();%0A  %7D%0A%0A  @Override%0A  public boolean groupEquals(Command command) %7B%0A    if (command instanceof ResourceCommand) %7B%0A      ResourceCommand%3C?, ?%3E resourceCommand = (ResourceCommand%3C?, ?%3E) command;%0A      return resourceCommand.resource == resource%0A        && resourceCommand.operation.groupEquals(operation);%0A    %7D%0A    return false;%0A  %7D%0A%0A
   @Overr

Handle command grouping in ResourceCommand.