Home | History | Annotate | Download | only in util
      1 /*
      2  * Copyright (C) 2015 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.messaging.util;
     18 
     19 import android.support.v4.util.LongSparseArray;
     20 
     21 /**
     22  * A space saving set for long values using v4 compat LongSparseArray
     23  */
     24 public class LongSparseSet {
     25     private static final Object THE_ONLY_VALID_VALUE = new Object();
     26     private final LongSparseArray<Object> mSet = new LongSparseArray<Object>();
     27 
     28     public LongSparseSet() {
     29     }
     30 
     31     /**
     32      * @param key The element to check
     33      * @return True if the element is in the set, false otherwise
     34      */
     35     public boolean contains(long key) {
     36         if (mSet.get(key, null/*default*/) == THE_ONLY_VALID_VALUE) {
     37             return true;
     38         }
     39         return false;
     40     }
     41 
     42     /**
     43      * Add an element to the set
     44      *
     45      * @param key The element to add
     46      */
     47     public void add(long key) {
     48         mSet.put(key, THE_ONLY_VALID_VALUE);
     49     }
     50 
     51     /**
     52      * Remove an element from the set
     53      *
     54      * @param key The element to remove
     55      */
     56     public void remove(long key) {
     57         mSet.delete(key);
     58     }
     59 }
     60