001    /*
002     * Copyright 2010 the original author or authors.
003     * 
004     *  Licensed under the Apache License, Version 2.0 (the "License");
005     *  you may not use this file except in compliance with the License.
006     *  You may obtain a copy of the License at
007     *
008     *      http://www.apache.org/licenses/LICENSE-2.0
009     *
010     *  Unless required by applicable law or agreed to in writing, software
011     *  distributed under the License is distributed on an "AS IS" BASIS,
012     *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     *  See the License for the specific language governing permissions and
014     *  limitations under the License.
015     */
016    package com.hs.mail.imap.mailbox;
017    
018    /**
019     * Mailbox is a class that represents a mailbox for mail messages.
020     * 
021     * @author Won Chul Doh
022     * @since Feb 2, 2010
023     * 
024     */
025    public class Mailbox {
026    
027            /**
028             * Delimiter string that separates mailbox's pathname from the names of
029             * immediate sub-mailboxes.
030             */
031            public static String folderSeparator = ".";
032    
033            private long mailboxID;
034    
035            private String name;
036            
037            private long ownerID;
038            
039            private long nextUID;
040            
041            private long uidValidity;
042            
043            private boolean noInferiors = false;
044            
045            private boolean noSelect = false;
046            
047            private boolean readOnly = false;
048            
049            private boolean marked = false;
050            
051            private boolean hasChildren = false;
052    
053            public Mailbox() {
054                    super();
055            }
056    
057            public Mailbox(String name) {
058                    super();
059                    this.name = name;
060            }
061    
062            public long getMailboxID() {
063                    return mailboxID;
064            }
065    
066            public void setMailboxID(long mailboxID) {
067                    this.mailboxID = mailboxID;
068            }
069    
070            /**
071             * Returns the full name of this mailbox. If the mailbox resides under the
072             * root hierarchy, the returned name may contain the hierarchy delimiter.
073             * 
074             * @return name of the mailbox
075             */
076            public String getName() {
077                    return name;
078            }
079    
080            public void setName(String name) {
081                    this.name = name;
082            }
083    
084            public long getOwnerID() {
085                    return ownerID;
086            }
087    
088            public void setOwnerID(long ownerID) {
089                    this.ownerID = ownerID;
090            }
091    
092            /**
093             * Returns the predicted UID that will be assigned to the next message that
094             * is appended to this mailbox.
095             * 
096             * @return the UIDNEXT value
097             */
098            public long getNextUID() {
099                    return nextUID;
100            }
101    
102            public void setNextUID(long nextUid) {
103                    this.nextUID = nextUid;
104            }
105    
106            /**
107             * Returns the UIDValidity for this mailbox.
108             */
109            public long getUidValidity() {
110                    return uidValidity;
111            }
112    
113            public void setUidValidity(long uidValidity) {
114                    this.uidValidity = uidValidity;
115            }
116    
117            /**
118             * Check whether this mailbox can have child mailboxes.
119             */
120            public boolean isNoInferiors() {
121                    return noInferiors;
122            }
123    
124            public void setNoInferiors(boolean noInferiors) {
125                    this.noInferiors = noInferiors;
126            }
127    
128            /**
129             * Check whether this mailbox can be selected.
130             */
131            public boolean isNoSelect() {
132                    return noSelect;
133            }
134    
135            public void setNoSelect(boolean noSelect) {
136                    this.noSelect = noSelect;
137            }
138    
139            /**
140             * Check whether this mailbox is read-only.
141             */
142            public boolean isReadOnly() {
143                    return readOnly;
144            }
145    
146            public void setReadOnly(boolean readOnly) {
147                    this.readOnly = readOnly;
148            }
149    
150            public boolean isMarked() {
151                    return marked;
152            }
153    
154            public void setMarked(boolean marked) {
155                    this.marked = marked;
156            }
157    
158            /**
159             * Check whether this mailbox has child mailboxes.
160             */
161            public boolean hasChildren() {
162                    return hasChildren;
163            }
164    
165            public void setHasChildren(boolean hasChildren) {
166                    this.hasChildren = hasChildren;
167            }
168    
169            /**
170             * Check if this mailbox is subsequent child of the given mailbox.
171             * 
172             * @param mailbox
173             *            potential parent mailbox
174             * @return true if child, otherwise false
175             */
176            public boolean isChildOf(Mailbox mailbox) {
177                    return (mailbox != null) ? mailbox.getName().equals(getParent(name))
178                                    : false;
179            }
180    
181            public Mailbox rename(String base, String dest) {
182                    if (base.equals(name)) {
183                            name = dest;
184                    } else {
185                            name = dest + folderSeparator + name.substring(base.length());
186                    }
187                    return this;
188            }
189    
190            public boolean equals(Object obj) {
191                    if (obj instanceof Mailbox)
192                            return name.equals(((Mailbox) obj).getName());
193                    else if (obj instanceof String)
194                            return name.equals((String) obj);
195                    else
196                            return false;
197            }
198    
199            /**
200             * Get the full name of parent mailbox for given mailbox.
201             * 
202             * @param mailboxName
203             *            full name of the mailbox
204             * @return full name of parent mailbox
205             */
206            public static String getParent(String mailboxName) {
207                    int i;
208                    if ((i = mailboxName.lastIndexOf(Mailbox.folderSeparator)) != -1) {
209                            return mailboxName.substring(0, i);
210                    } else {
211                            return "";
212                    }
213            }
214    
215    }