1 /*
2  * Copyright (C) 2018 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.textclassifier.common.logging;
18 
19 import com.google.common.base.Preconditions;
20 import java.util.Locale;
21 import java.util.Objects;
22 import java.util.UUID;
23 
24 /** This class represents the id of a text classification session. */
25 public final class TextClassificationSessionId {
26   private final String value;
27 
28   /** Creates a new instance. */
TextClassificationSessionId()29   public TextClassificationSessionId() {
30     this(UUID.randomUUID().toString());
31   }
32 
TextClassificationSessionId(String value)33   private TextClassificationSessionId(String value) {
34     this.value = Preconditions.checkNotNull(value);
35   }
36 
37   @Override
toString()38   public String toString() {
39     return String.format(Locale.US, "TextClassificationSessionId {%s}", value);
40   }
41 
42   @Override
hashCode()43   public int hashCode() {
44     return value.hashCode();
45   }
46 
47   @Override
equals(Object o)48   public boolean equals(Object o) {
49     if (this == o) {
50       return true;
51     }
52     if (o == null || getClass() != o.getClass()) {
53       return false;
54     }
55     TextClassificationSessionId that = (TextClassificationSessionId) o;
56     return Objects.equals(value, that.value);
57   }
58 
59   /**
60    * Flattens this id to a string.
61    *
62    * @return The flattened id.
63    */
getValue()64   public String getValue() {
65     return value;
66   }
67 
68   /**
69    * Recovers a TextClassificationSessionId from a string of the form returned by {@link
70    * #getValue()}.
71    */
unflattenFromString(String value)72   public static TextClassificationSessionId unflattenFromString(String value) {
73     return new TextClassificationSessionId(value);
74   }
75 }
76