Home | History | Annotate | Download | only in db
      1 /*
      2  * Copyright (C) 2017 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.example.android.autofill.service.data.source.local.db;
     18 
     19 import android.arch.persistence.room.TypeConverter;
     20 
     21 import java.util.Arrays;
     22 import java.util.List;
     23 
     24 import static java.util.stream.Collectors.toList;
     25 
     26 /**
     27  * Type converter for Room database.
     28  */
     29 public class Converters {
     30 
     31     /**
     32      * If database returns a {@link String} containing a comma delimited list of ints, this converts
     33      * the {@link String} to an {@link IntList}.
     34      */
     35     @TypeConverter
     36     public static IntList storedStringToIntList(String value) {
     37         List<String> strings = Arrays.asList(value.split("\\s*,\\s*"));
     38         List<Integer> ints = strings.stream().map(Integer::parseInt).collect(toList());
     39         return new IntList(ints);
     40     }
     41 
     42     /**
     43      * Converts the {@link IntList} back into a String containing a comma delimited list of
     44      * ints.
     45      */
     46     @TypeConverter
     47     public static String intListToStoredString(IntList list) {
     48         StringBuilder stringBuilder = new StringBuilder();
     49         for (Integer integer : list.ints) {
     50             stringBuilder.append(integer).append(",");
     51         }
     52         return stringBuilder.toString();
     53     }
     54 
     55     /**
     56      * If database returns a {@link String} containing a comma delimited list of Strings, this
     57      * converts the {@link String} to a {@link StringList}.
     58      */
     59     @TypeConverter
     60     public static StringList storedStringToStringList(String value) {
     61         List<String> strings = Arrays.asList(value.split("\\s*,\\s*"));
     62         return new StringList(strings);
     63     }
     64 
     65 
     66     /**
     67      * Converts the {@link StringList} back into a {@link String} containing a comma delimited
     68      * list of {@link String}s.
     69      */
     70     @TypeConverter
     71     public static String stringListToStoredString(StringList list) {
     72         StringBuilder stringBuilder = new StringBuilder();
     73         for (String string : list.strings) {
     74             stringBuilder.append(string).append(",");
     75         }
     76         return stringBuilder.toString();
     77     }
     78 
     79     /**
     80      * Wrapper class for {@code List<Integer>} so it can work with Room type converters.
     81      */
     82     public static class IntList {
     83         public final List<Integer> ints;
     84 
     85         public IntList(List<Integer> ints) {
     86             this.ints = ints;
     87         }
     88     }
     89 
     90     /**
     91      * Wrapper class for {@code List<String>} so it can work with Room type converters.
     92      */
     93     public static class StringList {
     94         public final List<String> strings;
     95 
     96         public StringList(List<String> ints) {
     97             this.strings = ints;
     98         }
     99     }
    100 }
    101