Line data Source code
1 : // Copyright (C) 2008 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.entities; 16 : 17 : import com.google.auto.value.AutoValue; 18 : import com.google.gerrit.common.Nullable; 19 : 20 0 : public final class AccountGroup { 21 : public static NameKey nameKey(String n) { 22 154 : return new AutoValue_AccountGroup_NameKey(n); 23 : } 24 : 25 : /** Group name key */ 26 : @AutoValue 27 154 : public abstract static class NameKey implements Comparable<NameKey> { 28 : abstract String name(); 29 : 30 : public String get() { 31 154 : return name(); 32 : } 33 : 34 : @Override 35 : public final int compareTo(NameKey o) { 36 0 : return name().compareTo(o.name()); 37 : } 38 : 39 : @Override 40 : public final String toString() { 41 153 : return KeyUtil.encode(get()); 42 : } 43 : } 44 : 45 : public static UUID uuid(String n) { 46 157 : return new AutoValue_AccountGroup_UUID(n); 47 : } 48 : 49 : /** Globally unique identifier. */ 50 : @AutoValue 51 157 : public abstract static class UUID implements Comparable<UUID> { 52 : abstract String uuid(); 53 : 54 : public String get() { 55 156 : return uuid(); 56 : } 57 : 58 : /** Returns true if the UUID is for a group managed within Gerrit. */ 59 : public boolean isInternalGroup() { 60 33 : return get().matches("^[0-9a-f]{40}$"); 61 : } 62 : 63 : /** Parse an {@link AccountGroup.UUID} out of a string representation. */ 64 : public static UUID parse(String str) { 65 4 : return AccountGroup.uuid(KeyUtil.decode(str)); 66 : } 67 : 68 : /** Parse an {@link AccountGroup.UUID} out of a ref-name. */ 69 : @Nullable 70 : public static UUID fromRef(String ref) { 71 103 : if (ref == null) { 72 1 : return null; 73 : } 74 103 : if (ref.startsWith(RefNames.REFS_GROUPS)) { 75 41 : return fromRefPart(ref.substring(RefNames.REFS_GROUPS.length())); 76 : } 77 102 : return null; 78 : } 79 : 80 : /** 81 : * Parse an {@link AccountGroup.UUID} out of a part of a ref-name. 82 : * 83 : * @param refPart a ref name with the following syntax: {@code "12/1234..."}. We assume that the 84 : * caller has trimmed any prefix. 85 : */ 86 : @Nullable 87 : public static UUID fromRefPart(String refPart) { 88 41 : String uuid = RefNames.parseShardedUuidFromRefPart(refPart); 89 41 : return uuid != null ? AccountGroup.uuid(uuid) : null; 90 : } 91 : 92 : @Override 93 : public final int compareTo(UUID o) { 94 0 : return uuid().compareTo(o.uuid()); 95 : } 96 : 97 : @Override 98 : public final String toString() { 99 51 : return KeyUtil.encode(get()); 100 : } 101 : } 102 : 103 : public static Id id(int id) { 104 154 : return new AutoValue_AccountGroup_Id(id); 105 : } 106 : 107 : /** Synthetic key to link to within the database */ 108 : @AutoValue 109 154 : public abstract static class Id { 110 : abstract int id(); 111 : 112 : public int get() { 113 154 : return id(); 114 : } 115 : 116 : /** Parse an AccountGroup.Id out of a string representation. */ 117 : public static Id parse(String str) { 118 7 : return AccountGroup.id(Integer.parseInt(str)); 119 : } 120 : 121 : @Override 122 : public final String toString() { 123 8 : return Integer.toString(get()); 124 : } 125 : } 126 : }