Home | History | Annotate | Download | only in message
      1 /*
      2  * Copyright (C) 2009 Google Inc.  All rights reserved.
      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.google.polo.pairing.message;
     18 
     19 /**
     20  * Internal representation of a session encoding option.
     21  */
     22 public class EncodingOption {
     23 
     24   /**
     25    * Representation of a specific encoding type. The numeric values
     26    * should be kept synchronized with the constants defined by
     27    * Options.Encoding.EncodingType in polo.proto.
     28    */
     29   public enum EncodingType {
     30     ENCODING_UNKNOWN(0),
     31     ENCODING_ALPHANUMERIC(1),
     32     ENCODING_NUMERIC(2),
     33     ENCODING_HEXADECIMAL(3),
     34     ENCODING_QRCODE(4);
     35 
     36     private final int mIntVal;
     37 
     38     private EncodingType(int intVal) {
     39       mIntVal = intVal;
     40     }
     41 
     42     public static EncodingType fromIntVal(int intVal) {
     43       for (EncodingType encType : EncodingType.values()) {
     44         if (encType.getAsInt() == intVal) {
     45           return encType;
     46         }
     47       }
     48       return EncodingType.ENCODING_UNKNOWN;
     49     }
     50 
     51     public int getAsInt() {
     52       return mIntVal;
     53     }
     54   }
     55 
     56   private EncodingType mType;
     57 
     58   private int mSymbolLength;
     59 
     60   public EncodingOption(EncodingType type, int symbolLength) {
     61     mType = type;
     62     mSymbolLength = symbolLength;
     63   }
     64 
     65   @Override
     66   public String toString() {
     67     return mType + ":" + mSymbolLength;
     68   }
     69 
     70   public EncodingType getType() {
     71     return mType;
     72   }
     73 
     74   public int getSymbolLength() {
     75     return mSymbolLength;
     76   }
     77 
     78   @Override
     79   public boolean equals(Object obj) {
     80     if (this == obj) {
     81       return true;
     82     }
     83 
     84     if (!(obj instanceof EncodingOption)) {
     85       return false;
     86     }
     87 
     88     EncodingOption other = (EncodingOption) obj;
     89     if (mType == null) {
     90       if (other.mType != null) {
     91         return false;
     92       }
     93     } else if (!mType.equals(other.mType)) {
     94       return false;
     95     }
     96 
     97     return mSymbolLength == other.mSymbolLength;
     98   }
     99 
    100   @Override
    101   public int hashCode() {
    102     int result = 7;
    103     result = result * 31 + (mType != null ? mType.hashCode() : 0);
    104     result = result * 31 + mSymbolLength;
    105     return result;
    106   }
    107 
    108 }
    109