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 package com.android.messaging.util;
17 
18 import java.util.Arrays;
19 import java.util.Collection;
20 import java.util.HashSet;
21 
22 /**
23  * Utility class to make it easy to store multiple conversation id strings in a single string
24  * with delimeters.
25  */
26 public class ConversationIdSet extends HashSet<String> {
27     private static final String JOIN_DELIMITER = "|";
28     private static final String SPLIT_DELIMITER = "\\|";
29 
ConversationIdSet()30     public ConversationIdSet() {
31         super();
32     }
33 
ConversationIdSet(final Collection<String> asList)34     public ConversationIdSet(final Collection<String> asList) {
35         super(asList);
36     }
37 
first()38     public String first() {
39         if (size() > 0) {
40             return iterator().next();
41         } else {
42             return null;
43         }
44     }
45 
createSet(final String conversationIdSetString)46     public static ConversationIdSet createSet(final String conversationIdSetString) {
47         ConversationIdSet set = null;
48         if (conversationIdSetString != null) {
49             set = new ConversationIdSet(Arrays.asList(conversationIdSetString.split(
50                     SPLIT_DELIMITER)));
51         }
52         return set;
53     }
54 
getDelimitedString()55     public String getDelimitedString() {
56         return OsUtil.joinFromSetWithDelimiter(this, JOIN_DELIMITER);
57     }
58 
join(final String conversationIdSet1, final String conversationIdSet2)59     public static String join(final String conversationIdSet1, final String conversationIdSet2) {
60         String joined = null;
61         if (conversationIdSet1 == null) {
62             joined = conversationIdSet2;
63         } else if (conversationIdSet2 != null) {
64             joined = conversationIdSet1 + JOIN_DELIMITER + conversationIdSet2;
65         }
66         return joined;
67     }
68 
69 }
70