Home | History | Annotate | Download | only in textclassifier
      1 /*
      2  * Copyright (C) 2013 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 android.view.textclassifier;
     18 
     19 import android.annotation.NonNull;
     20 import android.os.Parcel;
     21 import android.os.Parcelable;
     22 
     23 import com.android.internal.util.Preconditions;
     24 
     25 import java.util.Locale;
     26 import java.util.UUID;
     27 
     28 /**
     29  * This class represents the id of a text classification session.
     30  */
     31 public final class TextClassificationSessionId implements Parcelable {
     32     private final @NonNull String mValue;
     33 
     34     /**
     35      * Creates a new instance.
     36      *
     37      * @hide
     38      */
     39     public TextClassificationSessionId() {
     40         this(UUID.randomUUID().toString());
     41     }
     42 
     43     /**
     44      * Creates a new instance.
     45      *
     46      * @param value The internal value.
     47      *
     48      * @hide
     49      */
     50     public TextClassificationSessionId(@NonNull String value) {
     51         mValue = value;
     52     }
     53 
     54     @Override
     55     public int hashCode() {
     56         final int prime = 31;
     57         int result = 1;
     58         result = prime * result + mValue.hashCode();
     59         return result;
     60     }
     61 
     62     @Override
     63     public boolean equals(Object obj) {
     64         if (this == obj) {
     65             return true;
     66         }
     67         if (obj == null) {
     68             return false;
     69         }
     70         if (getClass() != obj.getClass()) {
     71             return false;
     72         }
     73         TextClassificationSessionId other = (TextClassificationSessionId) obj;
     74         if (!mValue.equals(other.mValue)) {
     75             return false;
     76         }
     77         return true;
     78     }
     79 
     80     @Override
     81     public String toString() {
     82         return String.format(Locale.US, "TextClassificationSessionId {%s}", mValue);
     83     }
     84 
     85     @Override
     86     public void writeToParcel(Parcel parcel, int flags) {
     87         parcel.writeString(mValue);
     88     }
     89 
     90     @Override
     91     public int describeContents() {
     92         return 0;
     93     }
     94 
     95     /**
     96      * Flattens this id to a string.
     97      *
     98      * @return The flattened id.
     99      *
    100      * @hide
    101      */
    102     public @NonNull String flattenToString() {
    103         return mValue;
    104     }
    105 
    106     /**
    107      * Unflattens a print job id from a string.
    108      *
    109      * @param string The string.
    110      * @return The unflattened id, or null if the string is malformed.
    111      *
    112      * @hide
    113      */
    114     public static @NonNull TextClassificationSessionId unflattenFromString(@NonNull String string) {
    115         return new TextClassificationSessionId(string);
    116     }
    117 
    118     public static final Parcelable.Creator<TextClassificationSessionId> CREATOR =
    119             new Parcelable.Creator<TextClassificationSessionId>() {
    120                 @Override
    121                 public TextClassificationSessionId createFromParcel(Parcel parcel) {
    122                     return new TextClassificationSessionId(
    123                             Preconditions.checkNotNull(parcel.readString()));
    124                 }
    125 
    126                 @Override
    127                 public TextClassificationSessionId[] newArray(int size) {
    128                     return new TextClassificationSessionId[size];
    129                 }
    130             };
    131 }
    132