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.message.builder.ext;
017    
018    import java.util.LinkedList;
019    
020    import com.hs.mail.imap.message.builder.SearchRequestBuilder;
021    import com.hs.mail.imap.message.request.ImapRequest;
022    import com.hs.mail.imap.message.request.ext.SortRequest;
023    import com.hs.mail.imap.message.search.SearchKey;
024    import com.hs.mail.imap.message.search.SortKey;
025    import com.hs.mail.imap.parser.Token;
026    import com.hs.mail.imap.server.codec.ImapMessage;
027    
028    /**
029     * 
030     * @author Won Chul Doh
031     * @since 31 Oct, 2010
032     *
033     */
034    public class SortRequestBuilder extends SearchRequestBuilder {
035    
036            @Override
037            public ImapRequest createRequest(String tag, String command,
038                            ImapMessage message, boolean useUID) {
039                    LinkedList<Token> tokens = message.getTokens();
040                    String charset = parseCharset(tokens);
041                    SortKey sortKey = parseSortKey(tokens);
042                    SearchKey searchKey = createSearchKey(tag, tokens, charset);
043                    return new SortRequest(tag, command, charset, sortKey, searchKey,
044                                    useUID);
045            }
046    
047            private SortKey parseSortKey(LinkedList<Token> tokens) {
048                    Token token = tokens.remove();
049                    if ("REVERSE".equals(token.value)) {
050                            return new SortKey(tokens.remove().value, true);
051                    } else {
052                            return new SortKey(token.value, false);
053                    }
054            }
055            
056    }