|
| 1 | +/* |
| 2 | + * Copyright (c) "Neo4j" |
| 3 | + * Neo4j Sweden AB [http://neo4j.com] |
| 4 | + * |
| 5 | + * This file is part of Neo4j. |
| 6 | + * |
| 7 | + * Neo4j is free software: you can redistribute it and/or modify |
| 8 | + * it under the terms of the GNU General Public License as published by |
| 9 | + * the Free Software Foundation, either version 3 of the License, or |
| 10 | + * (at your option) any later version. |
| 11 | + * |
| 12 | + * This program is distributed in the hope that it will be useful, |
| 13 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | + * GNU General Public License for more details. |
| 16 | + * |
| 17 | + * You should have received a copy of the GNU General Public License |
| 18 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 19 | + */ |
| 20 | +package org.neo4j.gds.api; |
| 21 | + |
| 22 | +import org.neo4j.gds.Orientation; |
| 23 | +import org.neo4j.gds.api.schema.Direction; |
| 24 | + |
| 25 | +import java.util.Objects; |
| 26 | + |
| 27 | +/** |
| 28 | + * Graph characteristics describe certain capabilities of the graph. |
| 29 | + * <p> |
| 30 | + * Algorithms define the graph characteristics that they require to |
| 31 | + * for correct execution. The execution framework can use both, the |
| 32 | + * graph and the required characteristics to check if the algorithm |
| 33 | + * can be run on the given graph. |
| 34 | + */ |
| 35 | +public final class GraphCharacteristics { |
| 36 | + |
| 37 | + public static final GraphCharacteristics ALL; |
| 38 | + public static final GraphCharacteristics NONE; |
| 39 | + |
| 40 | + static { |
| 41 | + ALL = GraphCharacteristics.builder().directed().undirected().inverseIndexed().build(); |
| 42 | + NONE = GraphCharacteristics.builder().build(); |
| 43 | + } |
| 44 | + |
| 45 | + private static final int DIRECTED = 1; |
| 46 | + private static final int UNDIRECTED = 1 << 1; |
| 47 | + private static final int INVERSE_INDEXED = 1 << 2; |
| 48 | + |
| 49 | + // We use a single int value to store the characteristics. |
| 50 | + // Each bit represents a characteristic. Setting / Getting |
| 51 | + // a characteristic is performed by bit-wise Or / And. |
| 52 | + private final int characteristics; |
| 53 | + |
| 54 | + public static GraphCharacteristics.Builder builder() { |
| 55 | + return new Builder(); |
| 56 | + } |
| 57 | + |
| 58 | + private GraphCharacteristics(int characteristics) { |
| 59 | + this.characteristics = characteristics; |
| 60 | + } |
| 61 | + |
| 62 | + public boolean isDirected() { |
| 63 | + return (characteristics() & DIRECTED) == DIRECTED; |
| 64 | + } |
| 65 | + |
| 66 | + public boolean isUndirected() { |
| 67 | + return (characteristics() & UNDIRECTED) == UNDIRECTED; |
| 68 | + } |
| 69 | + |
| 70 | + public boolean isInverseIndexed() { |
| 71 | + return (characteristics() & INVERSE_INDEXED) == INVERSE_INDEXED; |
| 72 | + } |
| 73 | + |
| 74 | + private int characteristics() { |
| 75 | + return characteristics; |
| 76 | + } |
| 77 | + |
| 78 | + @Override |
| 79 | + public String toString() { |
| 80 | + return "GraphCharacteristics{" + |
| 81 | + "isDirected=" + isDirected() + |
| 82 | + ", isUndirected=" + isUndirected() + |
| 83 | + ", isInverseIndexed=" + isInverseIndexed() + |
| 84 | + '}'; |
| 85 | + } |
| 86 | + |
| 87 | + @Override |
| 88 | + public boolean equals(Object o) { |
| 89 | + if (this == o) return true; |
| 90 | + if (o == null || getClass() != o.getClass()) return false; |
| 91 | + GraphCharacteristics that = (GraphCharacteristics) o; |
| 92 | + return characteristics == that.characteristics; |
| 93 | + } |
| 94 | + |
| 95 | + @Override |
| 96 | + public int hashCode() { |
| 97 | + return Objects.hash(characteristics); |
| 98 | + } |
| 99 | + |
| 100 | + public static class Builder { |
| 101 | + private int characteristics = 0; |
| 102 | + |
| 103 | + public Builder directed() { |
| 104 | + this.characteristics |= GraphCharacteristics.DIRECTED; |
| 105 | + return this; |
| 106 | + } |
| 107 | + |
| 108 | + public Builder undirected() { |
| 109 | + this.characteristics |= GraphCharacteristics.UNDIRECTED; |
| 110 | + return this; |
| 111 | + } |
| 112 | + |
| 113 | + public Builder inverseIndexed() { |
| 114 | + this.characteristics |= GraphCharacteristics.INVERSE_INDEXED; |
| 115 | + return this; |
| 116 | + } |
| 117 | + |
| 118 | + public Builder withOrientation(Orientation orientation) { |
| 119 | + switch (orientation) { |
| 120 | + case NATURAL: |
| 121 | + case REVERSE: |
| 122 | + return this.directed(); |
| 123 | + case UNDIRECTED: |
| 124 | + return this.undirected(); |
| 125 | + default: |
| 126 | + throw new UnsupportedOperationException("Unexpected orientation: " + orientation); |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + public Builder withDirection(Direction direction) { |
| 131 | + switch (direction) { |
| 132 | + case DIRECTED: |
| 133 | + return this.directed(); |
| 134 | + case UNDIRECTED: |
| 135 | + return this.undirected(); |
| 136 | + default: |
| 137 | + throw new UnsupportedOperationException("Unexpected direction: " + direction); |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + public GraphCharacteristics build() { |
| 142 | + return new GraphCharacteristics(this.characteristics); |
| 143 | + } |
| 144 | + } |
| 145 | +} |
0 commit comments