1 /* 2 * Copyright 2017 Google Inc. 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 */ 16 package com.example.android.wearable.wear.messaging.model; 17 18 import android.os.Parcel; 19 import android.os.Parcelable; 20 21 /** Data representation of each message. Stores the sender, text content, and the sent time. */ 22 public class Message implements Parcelable { 23 private String id; 24 private String senderId; 25 private String name; 26 private String text; 27 private long sentTime; 28 Message()29 public Message() {} 30 Message(Message message, String name)31 public Message(Message message, String name) { 32 senderId = message.getSenderId(); 33 this.name = name; 34 text = message.getText(); 35 } 36 Message(Builder builder)37 private Message(Builder builder) { 38 id = builder.id; 39 senderId = builder.senderId; 40 text = builder.text; 41 } 42 getId()43 public String getId() { 44 return id; 45 } 46 setId(String id)47 public void setId(String id) { 48 this.id = id; 49 } 50 getSenderId()51 public String getSenderId() { 52 return senderId; 53 } 54 getText()55 public String getText() { 56 return text; 57 } 58 getSentTime()59 public long getSentTime() { 60 return sentTime; 61 } 62 setSentTime(long sentTime)63 public void setSentTime(long sentTime) { 64 this.sentTime = sentTime; 65 } 66 getName()67 public String getName() { 68 return name; 69 } 70 setName(String name)71 public void setName(String name) { 72 this.name = name; 73 } 74 75 /** Message Builder */ 76 public static final class Builder { 77 private String id; 78 private String senderId; 79 private String text; 80 Builder()81 public Builder() {} 82 id(String val)83 public Builder id(String val) { 84 id = val; 85 return this; 86 } 87 senderId(String val)88 public Builder senderId(String val) { 89 senderId = val; 90 return this; 91 } 92 text(String val)93 public Builder text(String val) { 94 text = val; 95 return this; 96 } 97 build()98 public Message build() { 99 return new Message(this); 100 } 101 } 102 103 /** 104 * Indicates whether some other object is "equal to" this one. 105 * 106 * @param other - the reference object with which to compare. 107 * @return true/false based on all fields being equal or equally null 108 */ 109 @Override equals(Object other)110 public boolean equals(Object other) { 111 if (this == other) { 112 return true; 113 } 114 if (other == null || getClass() != other.getClass()) { 115 return false; 116 } 117 118 Message message = (Message) other; 119 120 if (sentTime != message.sentTime) { 121 return false; 122 } 123 if (id != null ? !id.equals(message.id) : message.id != null) { 124 return false; 125 } 126 if (!senderId.equals(message.senderId)) { 127 return false; 128 } 129 if (name != null ? !name.equals(message.name) : message.name != null) { 130 return false; 131 } 132 return text != null ? text.equals(message.text) : message.text == null; 133 } 134 135 /** 136 * Returns a hash code value for the object. 137 * 138 * @return a hash code value for this object. 139 */ 140 @Override hashCode()141 public int hashCode() { 142 int result = id != null ? id.hashCode() : 0; 143 result = 31 * result + senderId.hashCode(); 144 result = 31 * result + (name != null ? name.hashCode() : 0); 145 result = 31 * result + (text != null ? text.hashCode() : 0); 146 result = 31 * result + (int) (sentTime ^ (sentTime >>> 32)); 147 return result; 148 } 149 150 @Override describeContents()151 public int describeContents() { 152 return 0; 153 } 154 155 @Override writeToParcel(Parcel dest, int flags)156 public void writeToParcel(Parcel dest, int flags) { 157 dest.writeString(this.senderId); 158 dest.writeString(this.name); 159 dest.writeString(this.text); 160 dest.writeLong(this.sentTime); 161 } 162 Message(Parcel in)163 protected Message(Parcel in) { 164 this.senderId = in.readString(); 165 this.name = in.readString(); 166 this.text = in.readString(); 167 this.sentTime = in.readLong(); 168 } 169 170 public static final Creator<Message> CREATOR = 171 new Creator<Message>() { 172 @Override 173 public Message createFromParcel(Parcel source) { 174 return new Message(source); 175 } 176 177 @Override 178 public Message[] newArray(int size) { 179 return new Message[size]; 180 } 181 }; 182 } 183