1/* Copyright 2017 The TensorFlow Authors. All Rights Reserved.
2
3Licensed under the Apache License, Version 2.0 (the "License");
4you may not use this file except in compliance with the License.
5You may obtain a copy of the License at
6
7    http://www.apache.org/licenses/LICENSE-2.0
8
9Unless required by applicable law or agreed to in writing, software
10distributed under the License is distributed on an "AS IS" BASIS,
11WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12See the License for the specific language governing permissions and
13limitations under the License.
14==============================================================================*/
15
16package org.tensorflow.op;
17
18import org.tensorflow.Operation;
19
20/**
21 * A base class for {@link Op} implementations that are backed by a single {@link Operation}.
22 *
23 * <p>Each operation registered in the TensorFlow core is a primitive and is provided as a {@code
24 * PrimitiveOp}. Custom operations working with only one primitive may also derive from this class.
25 */
26public abstract class PrimitiveOp implements Op {
27
28  @Override
29  public final int hashCode() {
30    return operation.hashCode();
31  }
32
33  @Override
34  public final boolean equals(Object obj) {
35    if (this == obj) {
36      return true;
37    }
38    // Note: we consider that all objects wrapping the same operation are equal, no matter their
39    // implementation
40    if (!(obj instanceof PrimitiveOp)) {
41      return false;
42    }
43    return operation.equals(((PrimitiveOp) obj).operation);
44  }
45
46  @Override
47  public final String toString() {
48    return String.format("<%s '%s'>", operation.type(), operation.name());
49  }
50
51  /**
52   * Underlying operation. It is deliberately not exposed by a getter method to avoid any name
53   * conflict with generated methods of the subclasses.
54   */
55  protected final Operation operation;
56
57  /**
58   * Constructor.
59   *
60   * @param operation the underlying operation
61   */
62  protected PrimitiveOp(Operation operation) {
63    this.operation = operation;
64  }
65}
66