Skip to content

Commit 6889c03

Browse files
author
Yegor Bugayenko
committed
#5 RtCounter optimized
1 parent 7694338 commit 6889c03

File tree

2 files changed

+31
-36
lines changed

2 files changed

+31
-36
lines changed

src/main/java/co/stateful/RtCounter.java

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import com.jcabi.aspects.Loggable;
3434
import com.jcabi.http.Request;
3535
import com.jcabi.http.response.RestResponse;
36+
import com.jcabi.http.response.XmlResponse;
3637
import com.jcabi.log.Logger;
3738
import java.io.IOException;
3839
import java.net.HttpURLConnection;
@@ -52,7 +53,7 @@
5253
@Immutable
5354
@Loggable(Loggable.DEBUG)
5455
@ToString(of = "label", includeFieldNames = false)
55-
@EqualsAndHashCode(of = { "srequest", "irequest" })
56+
@EqualsAndHashCode(of = { "label", "request" })
5657
final class RtCounter implements Counter {
5758

5859
/**
@@ -61,25 +62,18 @@ final class RtCounter implements Counter {
6162
private final transient String label;
6263

6364
/**
64-
* Set request.
65+
* Home request.
6566
*/
66-
private final transient Request srequest;
67-
68-
/**
69-
* Increment request.
70-
*/
71-
private final transient Request irequest;
67+
private final transient Request request;
7268

7369
/**
7470
* Ctor.
7571
* @param name Name of it
76-
* @param sreq SET request
77-
* @param ireq INC request
72+
* @param req Home page request
7873
*/
79-
RtCounter(final String name, final Request sreq, final Request ireq) {
74+
RtCounter(final String name, final Request req) {
8075
this.label = name;
81-
this.srequest = sreq;
82-
this.irequest = ireq;
76+
this.request = req;
8377
}
8478

8579
@Override
@@ -90,7 +84,7 @@ public String name() {
9084
@Override
9185
public void set(final long value) throws IOException {
9286
final long start = System.currentTimeMillis();
93-
this.srequest.method(Request.PUT)
87+
this.front("set").method(Request.PUT)
9488
.uri().queryParam("value", value).back()
9589
.fetch()
9690
.as(RestResponse.class)
@@ -106,7 +100,7 @@ public void set(final long value) throws IOException {
106100
public long incrementAndGet(final long delta) throws IOException {
107101
final long start = System.currentTimeMillis();
108102
final long value = Long.parseLong(
109-
this.irequest.method(Request.GET)
103+
this.front("increment").method(Request.GET)
110104
.uri().queryParam("value", delta).back()
111105
.header(HttpHeaders.ACCEPT, MediaType.TEXT_PLAIN)
112106
.fetch()
@@ -121,4 +115,25 @@ public long incrementAndGet(final long delta) throws IOException {
121115
);
122116
return value;
123117
}
118+
119+
/**
120+
* Get front request.
121+
* @param ops Operation
122+
* @return Request
123+
* @throws IOException If fails
124+
*/
125+
private Request front(final String ops) throws IOException {
126+
return this.request.fetch()
127+
.as(RestResponse.class)
128+
.assertStatus(HttpURLConnection.HTTP_OK)
129+
.as(XmlResponse.class)
130+
.rel(
131+
String.format(
132+
// @checkstyle LineLength (1 line)
133+
"/page/counters/counter[name='%s']/links/link[@rel='%s']/@href",
134+
this.label, ops
135+
)
136+
);
137+
}
138+
124139
}

src/main/java/co/stateful/RtCounters.java

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -123,26 +123,6 @@ public void delete(final String name) throws IOException {
123123

124124
@Override
125125
public Counter get(final String name) throws IOException {
126-
final XmlResponse rsp = this.request.fetch()
127-
.as(RestResponse.class)
128-
.assertStatus(HttpURLConnection.HTTP_OK)
129-
.as(XmlResponse.class);
130-
return new RtCounter(
131-
name,
132-
rsp.rel(
133-
String.format(
134-
// @checkstyle LineLength (1 line)
135-
"/page/counters/counter[name='%s']/links/link[@rel='set']/@href",
136-
name
137-
)
138-
),
139-
rsp.rel(
140-
String.format(
141-
// @checkstyle LineLength (1 line)
142-
"/page/counters/counter[name='%s']/links/link[@rel='increment']/@href",
143-
name
144-
)
145-
)
146-
);
126+
return new RtCounter(name, this.request);
147127
}
148128
}

0 commit comments

Comments
 (0)