FolderOperation.java revision 259df5b9e11908c8ef7c91483924891dd96b3c27
1/*
2 * Copyright (C) 2012 Google Inc.
3 * Licensed to The Android Open Source Project.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 *      http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18package com.android.mail.ui;
19
20import com.android.mail.providers.Folder;
21import com.google.common.base.Objects;
22import com.google.common.collect.ImmutableList;
23
24import java.util.ArrayList;
25import java.util.Collection;
26import java.util.Collections;
27import java.util.HashSet;
28
29public class FolderOperation {
30    /** An immutable, empty conversation list */
31    public static final Collection<FolderOperation> EMPTY = Collections.emptyList();
32    public Folder mFolder;
33    public boolean mAdd;
34
35    public FolderOperation(Folder folder, Boolean operation) {
36        mAdd = operation;
37        mFolder = folder;
38    }
39
40    /**
41     * Get all the unique folders associated with a set of folder operations.
42     * @param ops
43     * @return
44     */
45    public final static ArrayList<Folder> getFolders(Collection<FolderOperation> ops) {
46        HashSet<Folder> folders = new HashSet<Folder>();
47        for (FolderOperation op : ops) {
48            folders.add(op.mFolder);
49        }
50        return new ArrayList<Folder>(folders);
51    }
52
53    /**
54     * Returns a collection of a single FolderOperation. This method always
55     * returns a valid collection even if the input folder is null.
56     *
57     * @param in a FolderOperation, possibly null.
58     * @return a collection of the folder.
59     */
60    public static Collection<FolderOperation> listOf(FolderOperation in) {
61        final Collection<FolderOperation> target = (in == null) ? EMPTY : ImmutableList.of(in);
62        return target;
63    }
64
65    /**
66     * Return if a set of folder operations removes the specified folder or adds
67     * inbox to a trashed conversation, making it a destructive operation.
68     */
69    public static boolean isDestructive(Collection<FolderOperation> folderOps, Folder folder) {
70        for (FolderOperation op : folderOps) {
71            if (Objects.equal(op.mFolder, folder) && !op.mAdd) {
72                return true;
73            }
74            if (folder.isTrash() && op.mFolder.isInbox()) {
75                return true;
76            }
77        }
78        return false;
79    }
80}
81