1package com.android.mail.providers;
2
3import android.os.Parcel;
4import android.os.Parcelable;
5
6import com.google.common.collect.ImmutableList;
7
8import java.util.Collection;
9import java.util.Collections;
10import java.util.List;
11
12/**
13 * Simple class to encapsulate an immutable list of {@link Folder} objects, and handle serialization
14 * and de-serialization.
15 */
16public class FolderList implements Parcelable {
17
18    private static final FolderList EMPTY = new FolderList(Collections.<Folder> emptyList());
19
20    public final ImmutableList<Folder> folders;
21
22    // Private to reinforce the copyOf() API, which makes it more clear that creating a FolderList
23    // has copy overhead.
24    private FolderList(Collection<Folder> in) {
25        if (in == null) {
26            folders = ImmutableList.of();
27        } else {
28            folders = ImmutableList.copyOf(in);
29        }
30    }
31
32    public FolderList(Parcel in) {
33        folders = ImmutableList.copyOf(in.createTypedArrayList(Folder.CREATOR));
34    }
35
36    @Override
37    public int describeContents() {
38        return 0;
39    }
40
41    @Override
42    public void writeToParcel(Parcel dest, int flags) {
43        dest.writeTypedList(folders);
44    }
45
46    public byte[] toBlob() {
47        final Parcel p = Parcel.obtain();
48        writeToParcel(p, 0);
49        final byte[] result = p.marshall();
50        p.recycle();
51        return result;
52    }
53
54    /**
55     * Directly turns a list of {@link Folder}s into a byte-array. Avoids the
56     * list-copy overhead of {@link #copyOf(Collection)} + {@link #toBlob()}.
57     *
58     * @param in a list of Folders
59     * @return the marshalled byte-array form of a {@link FolderList}
60     */
61    public static byte[] listToBlob(List<Folder> in) {
62        final Parcel p = Parcel.obtain();
63        p.writeTypedList(in);
64        final byte[] result = p.marshall();
65        p.recycle();
66        return result;
67    }
68
69    public static FolderList fromBlob(byte[] blob) {
70        if (blob == null) {
71            return EMPTY;
72        }
73
74        final Parcel p = Parcel.obtain();
75        p.unmarshall(blob, 0, blob.length);
76        p.setDataPosition(0);
77        final FolderList result = CREATOR.createFromParcel(p);
78        p.recycle();
79        return result;
80    }
81
82    public static FolderList copyOf(Collection<Folder> in) {
83        return new FolderList(in);
84    }
85
86    @Override
87    public boolean equals(Object o) {
88        return folders.equals(o);
89    }
90
91    @Override
92    public int hashCode() {
93        return folders.hashCode();
94    }
95
96    public static final Creator<FolderList> CREATOR = new Creator<FolderList>() {
97
98        @Override
99        public FolderList createFromParcel(Parcel source) {
100            return new FolderList(source);
101        }
102
103        @Override
104        public FolderList[] newArray(int size) {
105            return new FolderList[size];
106        }
107    };
108
109}
110