summaryrefslogtreecommitdiff
path: root/java/com/android/voicemail/impl/mail/store/imap/ImapList.java
blob: e4a6ec0aca5c541f1941f4f472377d0082640a54 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
/*
 * Copyright (C) 2015 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.voicemail.impl.mail.store.imap;

import java.util.ArrayList;

/** Class represents an IMAP list. */
public class ImapList extends ImapElement {
  /** {@link ImapList} representing an empty list. */
  public static final ImapList EMPTY =
      new ImapList() {
        @Override
        public void destroy() {
          // Don't call super.destroy().
          // It's a shared object.  We don't want the mDestroyed to be set on this.
        }

        @Override
        void add(ImapElement e) {
          throw new RuntimeException();
        }
      };

  private ArrayList<ImapElement> mList = new ArrayList<ImapElement>();

  /* package */ void add(ImapElement e) {
    if (e == null) {
      throw new RuntimeException("Can't add null");
    }
    mList.add(e);
  }

  @Override
  public final boolean isString() {
    return false;
  }

  @Override
  public final boolean isList() {
    return true;
  }

  public final int size() {
    return mList.size();
  }

  public final boolean isEmpty() {
    return size() == 0;
  }

  /**
   * Return true if the element at {@code index} exists, is string, and equals to {@code s}. (case
   * insensitive)
   */
  public final boolean is(int index, String s) {
    return is(index, s, false);
  }

  /** Same as {@link #is(int, String)}, but does the prefix match if {@code prefixMatch}. */
  public final boolean is(int index, String s, boolean prefixMatch) {
    if (!prefixMatch) {
      return getStringOrEmpty(index).is(s);
    } else {
      return getStringOrEmpty(index).startsWith(s);
    }
  }

  /**
   * Return the element at {@code index}. If {@code index} is out of range, returns {@link
   * ImapElement#NONE}.
   */
  public final ImapElement getElementOrNone(int index) {
    return (index >= mList.size()) ? ImapElement.NONE : mList.get(index);
  }

  /**
   * Return the element at {@code index} if it's a list. If {@code index} is out of range or not a
   * list, returns {@link ImapList#EMPTY}.
   */
  public final ImapList getListOrEmpty(int index) {
    ImapElement el = getElementOrNone(index);
    return el.isList() ? (ImapList) el : EMPTY;
  }

  /**
   * Return the element at {@code index} if it's a string. If {@code index} is out of range or not a
   * string, returns {@link ImapString#EMPTY}.
   */
  public final ImapString getStringOrEmpty(int index) {
    ImapElement el = getElementOrNone(index);
    return el.isString() ? (ImapString) el : ImapString.EMPTY;
  }

  /**
   * Return an element keyed by {@code key}. Return null if not found. {@code key} has to be at an
   * even index.
   */
  /* package */ final ImapElement getKeyedElementOrNull(String key, boolean prefixMatch) {
    for (int i = 1; i < size(); i += 2) {
      if (is(i - 1, key, prefixMatch)) {
        return mList.get(i);
      }
    }
    return null;
  }

  /**
   * Return an {@link ImapList} keyed by {@code key}. Return {@link ImapList#EMPTY} if not found.
   */
  public final ImapList getKeyedListOrEmpty(String key) {
    return getKeyedListOrEmpty(key, false);
  }

  /**
   * Return an {@link ImapList} keyed by {@code key}. Return {@link ImapList#EMPTY} if not found.
   */
  public final ImapList getKeyedListOrEmpty(String key, boolean prefixMatch) {
    ImapElement e = getKeyedElementOrNull(key, prefixMatch);
    return (e != null) ? ((ImapList) e) : ImapList.EMPTY;
  }

  /**
   * Return an {@link ImapString} keyed by {@code key}. Return {@link ImapString#EMPTY} if not
   * found.
   */
  public final ImapString getKeyedStringOrEmpty(String key) {
    return getKeyedStringOrEmpty(key, false);
  }

  /**
   * Return an {@link ImapString} keyed by {@code key}. Return {@link ImapString#EMPTY} if not
   * found.
   */
  public final ImapString getKeyedStringOrEmpty(String key, boolean prefixMatch) {
    ImapElement e = getKeyedElementOrNull(key, prefixMatch);
    return (e != null) ? ((ImapString) e) : ImapString.EMPTY;
  }

  /** Return true if it contains {@code s}. */
  public final boolean contains(String s) {
    for (int i = 0; i < size(); i++) {
      if (getStringOrEmpty(i).is(s)) {
        return true;
      }
    }
    return false;
  }

  @Override
  public void destroy() {
    if (mList != null) {
      for (ImapElement e : mList) {
        e.destroy();
      }
      mList = null;
    }
    super.destroy();
  }

  @Override
  public String toString() {
    return mList.toString();
  }

  /** Return the text representations of the contents concatenated with ",". */
  public final String flatten() {
    return flatten(new StringBuilder()).toString();
  }

  /**
   * Returns text representations (i.e. getString()) of contents joined together with "," as the
   * separator.
   *
   * <p>Only used for building the capability string passed to vendor policies.
   *
   * <p>We can't use toString(), because it's for debugging (meaning the format may change any
   * time), and it won't expand literals.
   */
  private final StringBuilder flatten(StringBuilder sb) {
    sb.append('[');
    for (int i = 0; i < mList.size(); i++) {
      if (i > 0) {
        sb.append(',');
      }
      final ImapElement e = getElementOrNone(i);
      if (e.isList()) {
        getListOrEmpty(i).flatten(sb);
      } else if (e.isString()) {
        sb.append(getStringOrEmpty(i).getString());
      }
    }
    sb.append(']');
    return sb;
  }

  @Override
  public boolean equalsForTest(ImapElement that) {
    if (!super.equalsForTest(that)) {
      return false;
    }
    ImapList thatList = (ImapList) that;
    if (size() != thatList.size()) {
      return false;
    }
    for (int i = 0; i < size(); i++) {
      if (!mList.get(i).equalsForTest(thatList.getElementOrNone(i))) {
        return false;
      }
    }
    return true;
  }
}