1/*
2 * Copyright (C) 2007 Esmertec AG.
3 * Copyright (C) 2007 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.google.android.mms.pdu;
19
20import java.util.HashMap;
21import java.util.Map;
22import java.util.Vector;
23
24public class PduBody {
25    private Vector<PduPart> mParts = null;
26
27    private Map<String, PduPart> mPartMapByContentId = null;
28    private Map<String, PduPart> mPartMapByContentLocation = null;
29    private Map<String, PduPart> mPartMapByName = null;
30    private Map<String, PduPart> mPartMapByFileName = null;
31
32    /**
33     * Constructor.
34     */
35    public PduBody() {
36        mParts = new Vector<PduPart>();
37
38        mPartMapByContentId = new HashMap<String, PduPart>();
39        mPartMapByContentLocation  = new HashMap<String, PduPart>();
40        mPartMapByName = new HashMap<String, PduPart>();
41        mPartMapByFileName = new HashMap<String, PduPart>();
42    }
43
44    private void putPartToMaps(PduPart part) {
45        // Put part to mPartMapByContentId.
46        byte[] contentId = part.getContentId();
47        if(null != contentId) {
48            mPartMapByContentId.put(new String(contentId), part);
49        }
50
51        // Put part to mPartMapByContentLocation.
52        byte[] contentLocation = part.getContentLocation();
53        if(null != contentLocation) {
54            String clc = new String(contentLocation);
55            mPartMapByContentLocation.put(clc, part);
56        }
57
58        // Put part to mPartMapByName.
59        byte[] name = part.getName();
60        if(null != name) {
61            String clc = new String(name);
62            mPartMapByName.put(clc, part);
63        }
64
65        // Put part to mPartMapByFileName.
66        byte[] fileName = part.getFilename();
67        if(null != fileName) {
68            String clc = new String(fileName);
69            mPartMapByFileName.put(clc, part);
70        }
71    }
72
73    /**
74     * Appends the specified part to the end of this body.
75     *
76     * @param part part to be appended
77     * @return true when success, false when fail
78     * @throws NullPointerException when part is null
79     */
80    public boolean addPart(PduPart part) {
81        if(null == part) {
82            throw new NullPointerException();
83        }
84
85        putPartToMaps(part);
86        return mParts.add(part);
87    }
88
89    /**
90     * Inserts the specified part at the specified position.
91     *
92     * @param index index at which the specified part is to be inserted
93     * @param part part to be inserted
94     * @throws NullPointerException when part is null
95     */
96    public void addPart(int index, PduPart part) {
97        if(null == part) {
98            throw new NullPointerException();
99        }
100
101        putPartToMaps(part);
102        mParts.add(index, part);
103    }
104
105    /**
106     * Removes the part at the specified position.
107     *
108     * @param index index of the part to return
109     * @return part at the specified index
110     */
111    public PduPart removePart(int index) {
112        return mParts.remove(index);
113    }
114
115    /**
116     * Remove all of the parts.
117     */
118    public void removeAll() {
119        mParts.clear();
120    }
121
122    /**
123     * Get the part at the specified position.
124     *
125     * @param index index of the part to return
126     * @return part at the specified index
127     */
128    public PduPart getPart(int index) {
129        return mParts.get(index);
130    }
131
132    /**
133     * Get the index of the specified part.
134     *
135     * @param part the part object
136     * @return index the index of the first occurrence of the part in this body
137     */
138    public int getPartIndex(PduPart part) {
139        return mParts.indexOf(part);
140    }
141
142    /**
143     * Get the number of parts.
144     *
145     * @return the number of parts
146     */
147    public int getPartsNum() {
148        return mParts.size();
149    }
150
151    /**
152     * Get pdu part by content id.
153     *
154     * @param cid the value of content id.
155     * @return the pdu part.
156     */
157    public PduPart getPartByContentId(String cid) {
158        return mPartMapByContentId.get(cid);
159    }
160
161    /**
162     * Get pdu part by Content-Location. Content-Location of part is
163     * the same as filename and name(param of content-type).
164     *
165     * @param fileName the value of filename.
166     * @return the pdu part.
167     */
168    public PduPart getPartByContentLocation(String contentLocation) {
169        return mPartMapByContentLocation.get(contentLocation);
170    }
171
172    /**
173     * Get pdu part by name.
174     *
175     * @param fileName the value of filename.
176     * @return the pdu part.
177     */
178    public PduPart getPartByName(String name) {
179        return mPartMapByName.get(name);
180    }
181
182    /**
183     * Get pdu part by filename.
184     *
185     * @param fileName the value of filename.
186     * @return the pdu part.
187     */
188    public PduPart getPartByFileName(String filename) {
189        return mPartMapByFileName.get(filename);
190    }
191}
192