summaryrefslogtreecommitdiffstats
path: root/gerrit-common/src/main/java/com/google/gerrit/common/data/GroupReference.java
blob: f05d1b900718b4a1137cd0504137104b681f6cb2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// Copyright (C) 2010 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.gerrit.common.data;

import com.google.gerrit.reviewdb.client.AccountGroup;

/** Describes a group within a projects {@link AccessSection}s. */
public class GroupReference implements Comparable<GroupReference> {
  /** @return a new reference to the given group description. */
  public static GroupReference forGroup(AccountGroup group) {
    return new GroupReference(group.getGroupUUID(), group.getName());
  }

  protected String uuid;
  protected String name;

  protected GroupReference() {
  }

  public GroupReference(AccountGroup.UUID uuid, String name) {
    setUUID(uuid);
    setName(name);
  }

  public AccountGroup.UUID getUUID() {
    return uuid != null ? new AccountGroup.UUID(uuid) : null;
  }

  public void setUUID(AccountGroup.UUID newUUID) {
    uuid = newUUID != null ? newUUID.get() : null;
  }

  public String getName() {
    return name;
  }

  public void setName(String newName) {
    this.name = newName;
  }

  @Override
  public int compareTo(GroupReference o) {
    return uuid(this).compareTo(uuid(o));
  }

  private static String uuid(GroupReference a) {
    return a.getUUID() != null ? a.getUUID().get() : "?";
  }

  @Override
  public int hashCode() {
    return uuid(this).hashCode();
  }

  @Override
  public boolean equals(Object o) {
    return o instanceof GroupReference && compareTo((GroupReference) o) == 0;
  }

  @Override
  public String toString() {
    return "Group[" + getName() + " / " + getUUID() + "]";
  }
}