Line data Source code
1 : // Copyright (C) 2016 The Android Open Source Project 2 : // 3 : // Licensed under the Apache License, Version 2.0 (the "License"); 4 : // you may not use this file except in compliance with the License. 5 : // You may obtain a copy of the License at 6 : // 7 : // http://www.apache.org/licenses/LICENSE-2.0 8 : // 9 : // Unless required by applicable law or agreed to in writing, software 10 : // distributed under the License is distributed on an "AS IS" BASIS, 11 : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 : // See the License for the specific language governing permissions and 13 : // limitations under the License. 14 : 15 : package com.google.gerrit.server.patch; 16 : 17 : import static org.eclipse.jgit.lib.ObjectIdSerializer.read; 18 : import static org.eclipse.jgit.lib.ObjectIdSerializer.readWithoutMarker; 19 : import static org.eclipse.jgit.lib.ObjectIdSerializer.write; 20 : import static org.eclipse.jgit.lib.ObjectIdSerializer.writeWithoutMarker; 21 : 22 : import com.google.gerrit.extensions.client.DiffPreferencesInfo.Whitespace; 23 : import java.io.IOException; 24 : import java.io.ObjectInputStream; 25 : import java.io.ObjectOutputStream; 26 : import java.io.Serializable; 27 : import java.util.Objects; 28 : import org.eclipse.jgit.lib.ObjectId; 29 : 30 : public class DiffSummaryKey implements Serializable { 31 : public static final long serialVersionUID = 1L; 32 : 33 : /** see PatchListKey#oldId */ 34 : private transient ObjectId oldId; 35 : 36 : /** see PatchListKey#parentNum */ 37 : private transient Integer parentNum; 38 : 39 : private transient ObjectId newId; 40 : private transient Whitespace whitespace; 41 : 42 : public static DiffSummaryKey fromPatchListKey(PatchListKey plk) { 43 103 : return new DiffSummaryKey( 44 103 : plk.getOldId(), plk.getParentNum(), plk.getNewId(), plk.getWhitespace()); 45 : } 46 : 47 103 : private DiffSummaryKey(ObjectId oldId, Integer parentNum, ObjectId newId, Whitespace whitespace) { 48 103 : this.oldId = oldId; 49 103 : this.parentNum = parentNum; 50 103 : this.newId = newId; 51 103 : this.whitespace = whitespace; 52 103 : } 53 : 54 : PatchListKey toPatchListKey() { 55 103 : return new PatchListKey(oldId, parentNum, newId, whitespace); 56 : } 57 : 58 : @Override 59 : public int hashCode() { 60 103 : return Objects.hash(oldId, parentNum, newId, whitespace); 61 : } 62 : 63 : @Override 64 : public boolean equals(Object o) { 65 97 : if (o instanceof DiffSummaryKey) { 66 97 : DiffSummaryKey k = (DiffSummaryKey) o; 67 97 : return Objects.equals(oldId, k.oldId) 68 97 : && Objects.equals(parentNum, k.parentNum) 69 97 : && Objects.equals(newId, k.newId) 70 : && whitespace == k.whitespace; 71 : } 72 0 : return false; 73 : } 74 : 75 : @Override 76 : public String toString() { 77 0 : StringBuilder n = new StringBuilder(); 78 0 : n.append("DiffSummaryKey["); 79 0 : n.append(oldId != null ? oldId.name() : "BASE"); 80 0 : n.append(".."); 81 0 : n.append(newId.name()); 82 0 : n.append(" "); 83 0 : if (parentNum != null) { 84 0 : n.append(parentNum); 85 0 : n.append(" "); 86 : } 87 0 : n.append(whitespace.name()); 88 0 : n.append("]"); 89 0 : return n.toString(); 90 : } 91 : 92 : private void writeObject(ObjectOutputStream out) throws IOException { 93 6 : write(out, oldId); 94 6 : out.writeInt(parentNum == null ? 0 : parentNum); 95 6 : writeWithoutMarker(out, newId); 96 6 : Character c = PatchListKey.WHITESPACE_TYPES.get(whitespace); 97 6 : if (c == null) { 98 0 : throw new IOException("Invalid whitespace type: " + whitespace); 99 : } 100 6 : out.writeChar(c); 101 6 : } 102 : 103 : private void readObject(ObjectInputStream in) throws IOException { 104 1 : oldId = read(in); 105 1 : int n = in.readInt(); 106 1 : parentNum = n == 0 ? null : Integer.valueOf(n); 107 1 : newId = readWithoutMarker(in); 108 1 : char t = in.readChar(); 109 1 : whitespace = PatchListKey.WHITESPACE_TYPES.inverse().get(t); 110 1 : if (whitespace == null) { 111 0 : throw new IOException("Invalid whitespace type code: " + t); 112 : } 113 1 : } 114 : }