Line data Source code
1 : // Copyright (C) 2013 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.change; 16 : 17 : import com.google.auto.value.AutoValue; 18 : import com.google.gerrit.entities.BranchNameKey; 19 : import com.google.gerrit.entities.Change; 20 : import com.google.gerrit.entities.Project; 21 : import com.google.gerrit.extensions.restapi.Url; 22 : import java.util.Optional; 23 : 24 : @AutoValue 25 18 : public abstract class ChangeTriplet { 26 : public static String format(Change change) { 27 4 : return format(change.getDest(), change.getKey()); 28 : } 29 : 30 : private static String format(BranchNameKey branch, Change.Key change) { 31 4 : return branch.project().get() + "~" + branch.shortName() + "~" + change.get(); 32 : } 33 : 34 : /** 35 : * Parse a triplet out of a string. 36 : * 37 : * @param triplet string of the form "project~branch~id". 38 : * @return the triplet if the input string has the proper format, or absent if not. 39 : */ 40 : public static Optional<ChangeTriplet> parse(String triplet) { 41 19 : int z = triplet.lastIndexOf('~'); 42 19 : int y = triplet.lastIndexOf('~', z - 1); 43 19 : return parse(triplet, y, z); 44 : } 45 : 46 : public static Optional<ChangeTriplet> parse(String triplet, int y, int z) { 47 26 : if (y < 0 || z < 0) { 48 19 : return Optional.empty(); 49 : } 50 : 51 18 : String project = Url.decode(triplet.substring(0, y)); 52 18 : String branch = Url.decode(triplet.substring(y + 1, z)); 53 18 : String changeId = Url.decode(triplet.substring(z + 1)); 54 18 : return Optional.of( 55 : new AutoValue_ChangeTriplet( 56 18 : BranchNameKey.create(Project.nameKey(project), branch), Change.key(changeId))); 57 : } 58 : 59 : public final Project.NameKey project() { 60 4 : return branch().project(); 61 : } 62 : 63 : public abstract BranchNameKey branch(); 64 : 65 : public abstract Change.Key id(); 66 : 67 : @Override 68 : public final String toString() { 69 0 : return format(branch(), id()); 70 : } 71 : }