Skip to content

Commit 20f83c6

Browse files
More code tidy-up
1 parent ac9cdaa commit 20f83c6

8 files changed

Lines changed: 117 additions & 82 deletions

File tree

src/main/java/engineer/nightowl/sonos/api/domain/SonosMetadataStatus.java

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,98 @@ public class SonosMetadataStatus
99
private SonosItem currentItem;
1010
private SonosItem nextItem;
1111
private String streamInfo;
12+
13+
public SonosMetadataStatus() {
14+
}
15+
16+
public SonosMetadataStatus(SonosMusicContainer container, SonosItem currentItem, SonosItem nextItem,
17+
String streamInfo) {
18+
this.container = container;
19+
this.currentItem = currentItem;
20+
this.nextItem = nextItem;
21+
this.streamInfo = streamInfo;
22+
}
23+
24+
public SonosMusicContainer getContainer() {
25+
return container;
26+
}
27+
28+
public void setContainer(SonosMusicContainer container) {
29+
this.container = container;
30+
}
31+
32+
public SonosItem getCurrentItem() {
33+
return currentItem;
34+
}
35+
36+
public void setCurrentItem(SonosItem currentItem) {
37+
this.currentItem = currentItem;
38+
}
39+
40+
public SonosItem getNextItem() {
41+
return nextItem;
42+
}
43+
44+
public void setNextItem(SonosItem nextItem) {
45+
this.nextItem = nextItem;
46+
}
47+
48+
public String getStreamInfo() {
49+
return streamInfo;
50+
}
51+
52+
public void setStreamInfo(String streamInfo) {
53+
this.streamInfo = streamInfo;
54+
}
55+
56+
@Override
57+
public int hashCode() {
58+
final int prime = 31;
59+
int result = 1;
60+
result = prime * result + ((container == null) ? 0 : container.hashCode());
61+
result = prime * result + ((currentItem == null) ? 0 : currentItem.hashCode());
62+
result = prime * result + ((nextItem == null) ? 0 : nextItem.hashCode());
63+
result = prime * result + ((streamInfo == null) ? 0 : streamInfo.hashCode());
64+
return result;
65+
}
66+
67+
@Override
68+
public boolean equals(Object obj) {
69+
if (this == obj)
70+
return true;
71+
if (obj == null)
72+
return false;
73+
if (getClass() != obj.getClass())
74+
return false;
75+
SonosMetadataStatus other = (SonosMetadataStatus) obj;
76+
if (container == null) {
77+
if (other.container != null)
78+
return false;
79+
} else if (!container.equals(other.container))
80+
return false;
81+
if (currentItem == null) {
82+
if (other.currentItem != null)
83+
return false;
84+
} else if (!currentItem.equals(other.currentItem))
85+
return false;
86+
if (nextItem == null) {
87+
if (other.nextItem != null)
88+
return false;
89+
} else if (!nextItem.equals(other.nextItem))
90+
return false;
91+
if (streamInfo == null) {
92+
if (other.streamInfo != null)
93+
return false;
94+
} else if (!streamInfo.equals(other.streamInfo))
95+
return false;
96+
return true;
97+
}
98+
99+
@Override
100+
public String toString() {
101+
return "SonosMetadataStatus [container=" + container + ", currentItem=" + currentItem + ", nextItem=" + nextItem
102+
+ ", streamInfo=" + streamInfo + "]";
103+
}
104+
105+
12106
}

src/main/java/engineer/nightowl/sonos/api/domain/SonosToken.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*/
99
public class SonosToken implements Serializable
1010
{
11-
11+
private static final long serialVersionUID = -7489553913061166913L;
1212
private String accessToken;
1313
private String tokenType;
1414
private String refreshToken;

src/main/java/engineer/nightowl/sonos/api/enums/SonosType.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ public enum SonosType
2626
sessionStatus(SonosPlaybackSessionStatus.class),
2727
playerVolume(SonosPlayerVolume.class);
2828

29-
private final Class clazz;
29+
private final Class<?> clazz;
3030

31-
SonosType(final Class clazz)
31+
SonosType(final Class<?> clazz)
3232
{
3333
this.clazz = clazz;
3434
}
@@ -48,7 +48,7 @@ public static List<SonosType> getErrorTypes()
4848
*
4949
* @return a {@link java.lang.Class} object.
5050
*/
51-
public Class getClazz()
51+
public Class<?> getClazz()
5252
{
5353
return clazz;
5454
}

src/main/java/engineer/nightowl/sonos/api/exception/SonosApiClientException.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
public class SonosApiClientException extends Exception
88
{
99

10+
private static final long serialVersionUID = -7303156575625750389L;
11+
1012
SonosApiClientException()
1113
{
1214
}

src/main/java/engineer/nightowl/sonos/api/exception/SonosApiError.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
*/
88
public class SonosApiError extends Exception
99
{
10+
11+
private static final long serialVersionUID = 7034540163075468346L;
1012
private SonosErrorCode errorCode;
1113
private String reason;
1214

src/main/java/engineer/nightowl/sonos/api/resource/BaseResource.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ <T> T callApi(final HttpUriRequest request, final Class<T> type) throws SonosApi
9090
}
9191

9292
logger.debug("Raw response from API: {}", response);
93-
logger.debug("Raw response content from API: {}", new String(bytes));
93+
logger.debug("Raw response content from API: {}", bytes);
9494

9595
// Get type from Sonos response - not always possible
9696
final SonosType sonosDeclaredClass = getTypeFromHeader(response);
@@ -232,7 +232,7 @@ <T, U> T postToApi(final Class<T> returnType, final String token, final String p
232232
final Boolean validationEnabled = apiClient.getConfiguration().isClientSideValidationEnabled();
233233
// If the content for the request has the ability to be validated, do so if enabled.
234234
// If the object is invalid, there's no point sending it to the API to be rejected.
235-
if (validationEnabled && content instanceof Validatable)
235+
if (Boolean.TRUE.equals(validationEnabled) && content instanceof Validatable)
236236
{
237237
((Validatable) content).validate();
238238
}

src/main/java/engineer/nightowl/sonos/api/util/SonosUtilityHelper.java

Lines changed: 9 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -5,36 +5,9 @@
55

66
public class SonosUtilityHelper
77
{
8-
// null/empty check code from: https://dzone.com/articles/consistent-way-doing-null-0
9-
10-
/**
11-
* This method returns true if the collection is null or is empty.
12-
*
13-
* @param collection to check if empty
14-
* @return true | false
15-
*/
16-
public static boolean isEmpty(final Collection<?> collection)
8+
private SonosUtilityHelper()
179
{
18-
if (collection == null || collection.isEmpty())
19-
{
20-
return true;
21-
}
22-
return false;
23-
}
24-
25-
/**
26-
* This method returns true of the map is null or is empty.
27-
*
28-
* @param map to check if empty
29-
* @return true | false
30-
*/
31-
public static boolean isEmpty(final Map<?, ?> map)
32-
{
33-
if (map == null || map.isEmpty())
34-
{
35-
return true;
36-
}
37-
return false;
10+
// Default private constructor
3811
}
3912

4013
/**
@@ -45,52 +18,23 @@ public static boolean isEmpty(final Map<?, ?> map)
4518
*/
4619
public static boolean isEmpty(final Object object)
4720
{
21+
if (object == null)
22+
{
23+
return true;
24+
}
4825
if (object instanceof Map)
4926
{
50-
return isEmpty((Map) object);
27+
return ((Map<?, ?>) object).isEmpty();
5128
}
5229
if (object instanceof String)
5330
{
54-
return isEmpty((String) object);
31+
return ((String) object).isEmpty();
5532
}
5633
if (object instanceof Collection)
5734
{
58-
return isEmpty((Collection) object);
59-
}
60-
if (object == null)
61-
{
62-
return true;
63-
}
64-
return false;
65-
}
66-
67-
/**
68-
* This method returns true if the input array is null or its length is zero.
69-
*
70-
* @param array to check if empty
71-
* @return true | false
72-
*/
73-
public static boolean isEmpty(final Object[] array)
74-
{
75-
if (array == null || array.length == 0)
76-
{
77-
return true;
35+
return ((Collection<?>) object).isEmpty();
7836
}
7937
return false;
8038
}
8139

82-
/**
83-
* This method returns true if the input string is null or its length is zero.
84-
*
85-
* @param string to check if empty
86-
* @return true | false
87-
*/
88-
public static boolean isEmpty(final String string)
89-
{
90-
if (string == null || string.trim().length() == 0)
91-
{
92-
return true;
93-
}
94-
return false;
95-
}
9640
}

src/test/java/engineer/nightowl/sonos/api/resource/BaseResourceTest.java

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,6 @@ public void testThatMainApiCallWorks() throws IOException, SonosApiClientExcepti
9494
options.setGroupingLatency(50);
9595

9696
// Mocks
97-
final HttpGet req = baseResource.getGetRequest("token123", "some/test");
9897
final HttpEntity entity = new StringEntity(new ObjectMapper().writeValueAsString(options), ContentType.APPLICATION_JSON);
9998

10099
final CloseableHttpResponse mockedResponse = mock(CloseableHttpResponse.class);
@@ -118,7 +117,6 @@ public void testSonosNotDeclaringTypeStillWorks() throws IOException, SonosApiCl
118117
options.setGroupingLatency(50);
119118

120119
// Mocks
121-
final HttpGet req = baseResource.getGetRequest("token123", "some/test");
122120
final HttpEntity entity = new StringEntity(new ObjectMapper().writeValueAsString(options), ContentType.APPLICATION_JSON);
123121

124122
final CloseableHttpResponse mockedResponse = mock(CloseableHttpResponse.class);
@@ -137,9 +135,6 @@ public void testSonosNotDeclaringTypeStillWorks() throws IOException, SonosApiCl
137135
@Test
138136
public void testAuthErrorThrown() throws IOException, SonosApiClientException, SonosApiError
139137
{
140-
// Mocks
141-
final HttpGet req = baseResource.getGetRequest("token123", "some/test");
142-
143138
final CloseableHttpResponse mockedResponse = mock(CloseableHttpResponse.class);
144139
final StatusLine sl = new BasicStatusLine(new ProtocolVersion("HTTP", 1, 1), 401, null);
145140
when(mockedResponse.getStatusLine()).thenReturn(sl);
@@ -163,9 +158,8 @@ public void testApiErrorHandledCorrectly() throws IOException, SonosApiClientExc
163158
error.setErrorCode(SonosErrorCode.ERROR_NOT_CAPABLE);
164159
error.setReason("Some test information");
165160

166-
// Mocks
167-
final HttpGet req = baseResource.getGetRequest("token123", "some/test");
168-
final HttpEntity entity = new StringEntity(new ObjectMapper().writeValueAsString(error), ContentType.APPLICATION_JSON);
161+
final HttpEntity entity = new StringEntity(new ObjectMapper().writeValueAsString(error),
162+
ContentType.APPLICATION_JSON);
169163

170164
final CloseableHttpResponse mockedResponse = mock(CloseableHttpResponse.class);
171165
when(mockedResponse.getEntity()).thenReturn(entity);
@@ -194,9 +188,8 @@ public void testApiMismatchHandledCorrectly() throws IOException, SonosApiClient
194188
options.setEnhanceDialog(true);
195189
options.setGroupingLatency(50);
196190

197-
// Mocks
198-
final HttpGet req = baseResource.getGetRequest("token123", "some/test");
199-
final HttpEntity entity = new StringEntity(new ObjectMapper().writeValueAsString(options), ContentType.APPLICATION_JSON);
191+
final HttpEntity entity = new StringEntity(new ObjectMapper().writeValueAsString(options),
192+
ContentType.APPLICATION_JSON);
200193

201194
final CloseableHttpResponse mockedResponse = mock(CloseableHttpResponse.class);
202195
when(mockedResponse.getEntity()).thenReturn(entity);

0 commit comments

Comments
 (0)