1 /*
2  * Copyright 2018 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  */
16 
17 package com.android.pump.db;
18 
19 import android.net.Uri;
20 
21 import androidx.annotation.AnyThread;
22 import androidx.annotation.NonNull;
23 import androidx.annotation.Nullable;
24 
25 import java.util.ArrayList;
26 import java.util.Collections;
27 import java.util.List;
28 
29 @AnyThread
30 public class Album {
31     private final long mId;
32 
33     // TODO(b/123706949) Lock mutable fields to ensure consistent updates
34     private String mTitle;
35     private String mDescription;
36     private Uri mAlbumArtUri;
37     private Artist mArtist;
38     private final List<Audio> mAudios = new ArrayList<>();
39     private boolean mLoaded;
40 
Album(long id)41     Album(long id) {
42         mId = id;
43     }
44 
getId()45     public long getId() {
46         return mId;
47     }
48 
getTitle()49     public @Nullable String getTitle() {
50         return mTitle;
51     }
52 
getAlbumArtUri()53     public @Nullable Uri getAlbumArtUri() {
54         return mAlbumArtUri;
55     }
56 
getArtist()57     public @Nullable Artist getArtist() {
58         return mArtist;
59     }
60 
getAudios()61     public @NonNull List<Audio> getAudios() {
62         return Collections.unmodifiableList(mAudios);
63     }
64 
getDescription()65     public @Nullable String getDescription() {
66         return mDescription;
67     }
68 
setAlbumArtUri(@onNull Uri albumArtUri)69     public boolean setAlbumArtUri(@NonNull Uri albumArtUri) {
70         if (albumArtUri.equals(mAlbumArtUri)) {
71             return false;
72         }
73         mAlbumArtUri = albumArtUri;
74         return true;
75     }
76 
setDescription(@onNull String description)77     public boolean setDescription(@NonNull String description) {
78         if (description.equals(mDescription)) {
79             return false;
80         }
81         mDescription = description;
82         return true;
83     }
84 
setTitle(@onNull String title)85     boolean setTitle(@NonNull String title) {
86         if (title.equals(mTitle)) {
87             return false;
88         }
89         mTitle = title;
90         return true;
91     }
92 
setArtist(@onNull Artist artist)93     boolean setArtist(@NonNull Artist artist) {
94         if (artist.equals(mArtist)) {
95             return false;
96         }
97         mArtist = artist;
98         return true;
99     }
100 
addAudio(@onNull Audio audio)101     boolean addAudio(@NonNull Audio audio) {
102         if (mAudios.contains(audio)) {
103             return false;
104         }
105         return mAudios.add(audio);
106     }
107 
isLoaded()108     boolean isLoaded() {
109         return mLoaded;
110     }
111 
setLoaded()112     void setLoaded() {
113         mLoaded = true;
114     }
115 
116     @Override
equals(@ullable Object obj)117     public final boolean equals(@Nullable Object obj) {
118         return obj instanceof Album && mId == ((Album) obj).mId;
119     }
120 
121     @Override
hashCode()122     public final int hashCode() {
123         return (int) (mId ^ (mId >>> 32));
124     }
125 }
126