PhonebookEntry.java revision c27337ca60901fad583a96e84973af6ba379f3ef
1/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package com.android.bluetooth.pbapclient;
17
18import com.android.vcard.VCardEntry;
19
20import java.util.ArrayList;
21import java.util.HashSet;
22import java.util.List;
23import java.util.Objects;
24
25/**
26 *  A simpler more public version of VCardEntry.
27 */
28public class PhonebookEntry {
29    public static class Name {
30        public String family;
31        public String given;
32        public String middle;
33        public String prefix;
34        public String suffix;
35
36        public Name() { }
37
38        @Override
39        public boolean equals(Object o) {
40            if (!(o instanceof Name)) {
41                return false;
42            }
43
44            Name n = ((Name) o);
45            return (Objects.equals(family, n.family) || family != null && family.equals(n.family)) &&
46                    (Objects.equals(given, n.given) ||  given != null && given.equals(n.given)) &&
47                    (Objects.equals(middle, n.middle) || middle != null && middle.equals(n.middle)) &&
48                    (Objects.equals(prefix, n.prefix) || prefix != null && prefix.equals(n.prefix)) &&
49                    (Objects.equals(suffix, n.suffix) || suffix != null && suffix.equals(n.suffix));
50        }
51
52        @Override
53        public int hashCode() {
54            int result = 23 * (family == null ? 0 : family.hashCode());
55            result = 23 * result + (given == null ? 0 : given.hashCode());
56            result = 23 * result + (middle == null ? 0 : middle.hashCode());
57            result = 23 * result + (prefix == null ? 0 : prefix.hashCode());
58            result = 23 * result + (suffix == null ? 0 : suffix.hashCode());
59            return result;
60        }
61
62        @Override
63        public String toString() {
64            StringBuilder sb = new StringBuilder();
65            sb.append("Name: { family: ");
66            sb.append(family);
67            sb.append(" given: ");
68            sb.append(given);
69            sb.append(" middle: ");
70            sb.append(middle);
71            sb.append(" prefix: ");
72            sb.append(prefix);
73            sb.append(" suffix: ");
74            sb.append(suffix);
75            sb.append(" }");
76            return sb.toString();
77        }
78    }
79
80    public static class Phone {
81        public int type;
82        public String number;
83
84        @Override
85        public boolean equals(Object o) {
86            if (!(o instanceof Phone)) {
87                return false;
88            }
89
90            Phone p = (Phone) o;
91            return (Objects.equals(number, p.number) || number != null && number.equals(p.number))
92                    && type == p.type;
93        }
94
95        @Override
96        public int hashCode() {
97            return 23 * type + number.hashCode();
98        }
99
100        @Override
101        public String toString() {
102            StringBuilder sb = new StringBuilder();
103            sb.append(" Phone: { number: ");
104            sb.append(number);
105            sb.append(" type: " + type);
106            sb.append(" }");
107            return sb.toString();
108        }
109    }
110
111    @Override
112    public boolean equals(Object object) {
113        if (object instanceof PhonebookEntry) {
114            return equals((PhonebookEntry) object);
115        }
116        return false;
117    }
118
119    public PhonebookEntry() {
120        name = new Name();
121        phones = new ArrayList<Phone>();
122    }
123
124    public PhonebookEntry(VCardEntry v) {
125        name = new Name();
126        phones = new ArrayList<Phone>();
127
128        VCardEntry.NameData n = v.getNameData();
129        name.family = n.getFamily();
130        name.given = n.getGiven();
131        name.middle = n.getMiddle();
132        name.prefix = n.getPrefix();
133        name.suffix = n.getSuffix();
134
135        List<VCardEntry.PhoneData> vp = v.getPhoneList();
136        if (vp == null || vp.isEmpty()) {
137            return;
138        }
139
140        for (VCardEntry.PhoneData p : vp) {
141            Phone phone = new Phone();
142            phone.type = p.getType();
143            phone.number = p.getNumber();
144            phones.add(phone);
145        }
146    }
147
148    private boolean equals(PhonebookEntry p) {
149        return name.equals(p.name) && phones.equals(p.phones);
150    }
151
152    @Override
153    public int hashCode() {
154        return name.hashCode() + 23 * phones.hashCode();
155    }
156
157    @Override
158    public String toString() {
159        StringBuilder sb = new StringBuilder();
160        sb.append("PhonebookEntry { id: ");
161        sb.append(id);
162        sb.append(" ");
163        sb.append(name.toString());
164        sb.append(phones.toString());
165        sb.append(" }");
166        return sb.toString();
167    }
168
169    public Name name;
170    public List<Phone> phones;
171    public String id;
172}
173