Home | History | Annotate | Download | only in imap
      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.voicemail.impl.mail.store.imap;
     18 
     19 /**
     20  * Class representing "element"s in IMAP responses.
     21  *
     22  * <p>Class hierarchy:
     23  *
     24  * <pre>
     25  * ImapElement
     26  *   |
     27  *   |-- ImapElement.NONE (for 'index out of range')
     28  *   |
     29  *   |-- ImapList (isList() == true)
     30  *   |   |
     31  *   |   |-- ImapList.EMPTY
     32  *   |   |
     33  *   |   --- ImapResponse
     34  *   |
     35  *   --- ImapString (isString() == true)
     36  *       |
     37  *       |-- ImapString.EMPTY
     38  *       |
     39  *       |-- ImapSimpleString
     40  *       |
     41  *       |-- ImapMemoryLiteral
     42  *       |
     43  *       --- ImapTempFileLiteral
     44  * </pre>
     45  */
     46 public abstract class ImapElement {
     47   /**
     48    * An element that is returned by {@link ImapList#getElementOrNone} to indicate an index is out of
     49    * range.
     50    */
     51   public static final ImapElement NONE =
     52       new ImapElement() {
     53         @Override
     54         public void destroy() {
     55           // Don't call super.destroy().
     56           // It's a shared object.  We don't want the mDestroyed to be set on this.
     57         }
     58 
     59         @Override
     60         public boolean isList() {
     61           return false;
     62         }
     63 
     64         @Override
     65         public boolean isString() {
     66           return false;
     67         }
     68 
     69         @Override
     70         public String toString() {
     71           return "[NO ELEMENT]";
     72         }
     73 
     74         @Override
     75         public boolean equalsForTest(ImapElement that) {
     76           return super.equalsForTest(that);
     77         }
     78       };
     79 
     80   private boolean destroyed = false;
     81 
     82   public abstract boolean isList();
     83 
     84   public abstract boolean isString();
     85 
     86   protected boolean isDestroyed() {
     87     return destroyed;
     88   }
     89 
     90   /**
     91    * Clean up the resources used by the instance. It's for removing a temp file used by {@link
     92    * ImapTempFileLiteral}.
     93    */
     94   public void destroy() {
     95     destroyed = true;
     96   }
     97 
     98   /** Throws {@link RuntimeException} if it's already destroyed. */
     99   protected final void checkNotDestroyed() {
    100     if (destroyed) {
    101       throw new RuntimeException("Already destroyed");
    102     }
    103   }
    104 
    105   /**
    106    * Return a string that represents this object; it's purely for the debug purpose. Don't mistake
    107    * it for {@link ImapString#getString}.
    108    *
    109    * <p>Abstract to force subclasses to implement it.
    110    */
    111   @Override
    112   public abstract String toString();
    113 
    114   /**
    115    * The equals implementation that is intended to be used only for unit testing. (Because it may be
    116    * heavy and has a special sense of "equal" for testing.)
    117    */
    118   public boolean equalsForTest(ImapElement that) {
    119     if (that == null) {
    120       return false;
    121     }
    122     return this.getClass() == that.getClass(); // Has to be the same class.
    123   }
    124 }
    125