Skip to content

Commit 7694338

Browse files
author
Yegor Bugayenko
committed
#5 RtLock optimized
1 parent 8ff3f4b commit 7694338

File tree

2 files changed

+29
-35
lines changed

2 files changed

+29
-35
lines changed

src/main/java/co/stateful/RtLock.java

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
import com.jcabi.aspects.Immutable;
3333
import com.jcabi.aspects.Loggable;
3434
import com.jcabi.http.Request;
35+
import com.jcabi.http.response.RestResponse;
36+
import com.jcabi.http.response.XmlResponse;
3537
import com.jcabi.log.Logger;
3638
import com.jcabi.manifests.Manifests;
3739
import java.io.IOException;
@@ -52,7 +54,7 @@
5254
@Immutable
5355
@Loggable(Loggable.DEBUG)
5456
@ToString(of = "lck", includeFieldNames = false)
55-
@EqualsAndHashCode(of = { "lck", "lrequest", "urequest" })
57+
@EqualsAndHashCode(of = { "lck", "request" })
5658
final class RtLock implements Lock {
5759

5860
/**
@@ -61,25 +63,18 @@ final class RtLock implements Lock {
6163
private final transient String lck;
6264

6365
/**
64-
* Lock request.
66+
* Locks home.
6567
*/
66-
private final transient Request lrequest;
67-
68-
/**
69-
* Unlock request.
70-
*/
71-
private final transient Request urequest;
68+
private final transient Request request;
7269

7370
/**
7471
* Ctor.
7572
* @param name Name of it
76-
* @param lreq Lock request
77-
* @param ureq Unlock request
73+
* @param req Lock request
7874
*/
79-
RtLock(final String name, final Request lreq, final Request ureq) {
75+
RtLock(final String name, final Request req) {
8076
this.lck = name;
81-
this.lrequest = lreq;
82-
this.urequest = ureq;
77+
this.request = req;
8378
}
8479

8580
@Override
@@ -110,7 +105,7 @@ public void unlock() throws IOException {
110105
@Override
111106
public boolean lock(final String label) throws IOException {
112107
final long start = System.currentTimeMillis();
113-
final boolean locked = this.lrequest
108+
final boolean locked = this.front("lock")
114109
.body().formParam("label", label).back()
115110
.fetch()
116111
.status() == HttpURLConnection.HTTP_SEE_OTHER;
@@ -125,7 +120,7 @@ public boolean lock(final String label) throws IOException {
125120
@Override
126121
public boolean unlock(final String label) throws IOException {
127122
final long start = System.currentTimeMillis();
128-
final boolean unlocked = this.urequest
123+
final boolean unlocked = this.front("unlock")
129124
.uri().queryParam("label", label).back()
130125
.fetch()
131126
.status() == HttpURLConnection.HTTP_SEE_OTHER;
@@ -136,4 +131,21 @@ public boolean unlock(final String label) throws IOException {
136131
return unlocked;
137132
}
138133

134+
/**
135+
* Get front request.
136+
* @param label Label
137+
* @return Request
138+
* @throws IOException If fails
139+
*/
140+
private Request front(final String label) throws IOException {
141+
return this.request
142+
.fetch()
143+
.as(RestResponse.class)
144+
.assertStatus(HttpURLConnection.HTTP_OK)
145+
.as(XmlResponse.class)
146+
.rel(String.format("/page/links/link[@rel='%s']/@href", label))
147+
.method(Request.GET)
148+
.uri().queryParam("name", this.lck).back();
149+
}
150+
139151
}

src/main/java/co/stateful/RtLocks.java

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -83,26 +83,8 @@ public boolean exists(final String name) throws IOException {
8383
}
8484

8585
@Override
86-
public Lock get(final String name) throws IOException {
87-
return new RtLock(
88-
name,
89-
this.request
90-
.fetch()
91-
.as(RestResponse.class)
92-
.assertStatus(HttpURLConnection.HTTP_OK)
93-
.as(XmlResponse.class)
94-
.rel("/page/links/link[@rel='lock']/@href")
95-
.method(Request.POST)
96-
.body().formParam("name", name).back(),
97-
this.request
98-
.fetch()
99-
.as(RestResponse.class)
100-
.assertStatus(HttpURLConnection.HTTP_OK)
101-
.as(XmlResponse.class)
102-
.rel("/page/links/link[@rel='unlock']/@href")
103-
.method(Request.GET)
104-
.uri().queryParam("name", name).back()
105-
);
86+
public Lock get(final String name) {
87+
return new RtLock(name, this.request);
10688
}
10789

10890
}

0 commit comments

Comments
 (0)