Skip to content

Commit

Permalink
(#918) After review fixes, attempt 2
Browse files Browse the repository at this point in the history
  • Loading branch information
fanifieiev committed Nov 20, 2019
1 parent b235b60 commit 39a6c72
Show file tree
Hide file tree
Showing 16 changed files with 302 additions and 99 deletions.
16 changes: 16 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,22 @@ SOFTWARE.
<artifactId>commons-text</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>com.google.code.findbugs</groupId>
<artifactId>annotations</artifactId>
<version>3.0.1</version>
<scope>provided</scope>
<exclusions>
<exclusion>
<groupId>net.jcip</groupId>
<artifactId>jcip-annotations</artifactId>
</exclusion>
<exclusion>
<groupId>com.google.code.findbugs</groupId>
<artifactId>jsr305</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<testResources>
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/org/takes/misc/EnglishLowerCase.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@
/**
* English lower case string representation.
* @since 0.33
* @todo #918:30min Remove this class in favour of
* {@link org.cactoos.text.Lowered} which does the same. Find all usages of
* this class in the project and replace by suggested
* {@link org.cactoos.text.Lowered} class.
*/
public final class EnglishLowerCase {

Expand Down
145 changes: 145 additions & 0 deletions src/main/java/org/takes/misc/InputStreamsEqual.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2014-2019 Yegor Bugayenko
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package org.takes.misc;

import java.io.InputStream;
import java.nio.ByteBuffer;
import java.nio.channels.Channels;
import java.nio.channels.ReadableByteChannel;
import org.cactoos.Scalar;
import org.cactoos.scalar.Not;

/**
* Scalar to compare equality of InputStream objects.
*
* <p>The class is immutable and thread-safe.
* @since 2.0
*/
public final class InputStreamsEqual implements Scalar<Boolean> {

/**
* First channel of the first InputStream.
*/
private final ReadableByteChannel fchannel;

/**
* Second channel of the second InputStream.
*/
private final ReadableByteChannel schannel;

/**
* Ctor.
* @param fstream First InputStream
* @param sstream Second InputStream
* @checkstyle ParameterNumberCheck (4 lines)
*/
public InputStreamsEqual(
final InputStream fstream, final InputStream sstream) {
this.fchannel = Channels.newChannel(fstream);
this.schannel = Channels.newChannel(sstream);
}

@Override
public Boolean value() throws Exception {
final ByteBuffer fbuf = ByteBuffer.allocateDirect(1024);
final ByteBuffer sbuf = ByteBuffer.allocateDirect(1024);
boolean equals = false;
while (true) {
final int fbytes = this.fchannel.read(fbuf);
final int sbytes = this.schannel.read(sbuf);
if (fbytes == -1 || sbytes == -1) {
equals = true;
break;
} else {
fbuf.flip();
sbuf.flip();
final Scalar<Boolean> match = new InputStreamsEqual.BytesMatch(
fbuf, fbytes, sbuf, sbytes
);
if (new Not(match).value()) {
equals = false;
break;
}
fbuf.compact();
sbuf.compact();
}
}
return equals;
}

/**
* Scalar that checks if all bytes in buffers are equal.
*/
private static final class BytesMatch implements Scalar<Boolean> {
/**
* First byte buffer.
*/
private final ByteBuffer fbuf;

/**
* Number of bytes read into first buffer.
*/
private final int fbytes;

/**
* Second byte buffer.
*/
private final ByteBuffer sbuf;

/**
* Number of bytes read into second buffer.
*/
private final int sbytes;

/**
* Ctor.
* @param fbuf First byte buffer.
* @param fbytes Number of bytes read into first buffer.
* @param sbuf Second byte buffer.
* @param sbytes Number of bytes read into second buffer
* @checkstyle ParameterNumberCheck (8 lines)
*/
BytesMatch(final ByteBuffer fbuf, final int fbytes,
final ByteBuffer sbuf, final int sbytes) {
this.fbuf = fbuf;
this.fbytes = fbytes;
this.sbuf = sbuf;
this.sbytes = sbytes;
}

@Override
public Boolean value() throws Exception {
boolean matches = true;
int idx = 0;
while (idx < Math.min(this.fbytes, this.sbytes)) {
if (this.fbuf.get() != this.sbuf.get()) {
matches = false;
break;
}
idx += 1;
}
return matches;
}
}
}
46 changes: 36 additions & 10 deletions src/main/java/org/takes/rq/RequestOf.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,17 @@
*/
package org.takes.rq;

import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;
import org.cactoos.scalar.And;
import org.cactoos.scalar.HashCode;
import org.cactoos.scalar.Or;
import org.cactoos.scalar.Unchecked;
import org.takes.Request;
import org.takes.Scalar;
import org.takes.misc.InputStreamsEqual;

/**
* This {@link Request} implementation provides a way to build a request
Expand All @@ -51,21 +58,17 @@ public final class RequestOf implements Request {
* @param head Iterable head value
* @param body InputStream body value
*/
public RequestOf(final Iterable<String> head,
final InputStream body) {
this(
() -> head,
() -> body
);
public RequestOf(final Iterable<String> head, final InputStream body) {
this(() -> head, () -> body);
}

/**
* Ctor.
* @param head Scalar to provide head value
* @param body Scalar to provide body value
*/
public RequestOf(final Scalar<Iterable<String>> head,
final Scalar<InputStream> body) {
public RequestOf(
final Scalar<Iterable<String>> head, final Scalar<InputStream> body) {
this.shead = head;
this.sbody = body;
}
Expand All @@ -81,12 +84,35 @@ public InputStream body() throws IOException {
}

@Override
@SuppressFBWarnings("EQ_UNUSUAL")
public boolean equals(final Object that) {
return this == that || RequestOf.class.equals(that.getClass());
return new Unchecked<>(
new Or(
() -> this == that,
new And(
() -> that != null,
() -> RequestOf.class.equals(that.getClass()),
() -> {
final RequestOf other = (RequestOf) that;
return new And(
() -> {
final Iterator<String> iter = other.head()
.iterator();
return new And(
(String hdr) -> hdr.equals(iter.next()),
this.head()
).value();
},
new InputStreamsEqual(this.body(), other.body())
).value();
}
)
)
).value();
}

@Override
public int hashCode() {
return 0;
return new HashCode(new Unchecked<>(this.shead::get).value()).value();
}
}
7 changes: 1 addition & 6 deletions src/main/java/org/takes/rq/RqFake.java
Original file line number Diff line number Diff line change
Expand Up @@ -109,11 +109,6 @@ public RqFake(final List<String> head, final byte[] body) {
* @param body Body
*/
public RqFake(final List<String> head, final InputStream body) {
super(
new RequestOf(
() -> Collections.unmodifiableList(head),
() -> body
)
);
super(new RequestOf(Collections.unmodifiableList(head), body));
}
}
7 changes: 1 addition & 6 deletions src/main/java/org/takes/rq/RqLengthAware.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,7 @@ public final class RqLengthAware extends RqWrap {
* @param req Original request
*/
public RqLengthAware(final Request req) {
super(
new RequestOf(
req::head,
() -> RqLengthAware.cap(req)
)
);
super(new RequestOf(req::head, () -> RqLengthAware.cap(req)));
}

/**
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/org/takes/rq/RqOnce.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@
* <p>The class is immutable and thread-safe.
*
* @since 0.36
* @todo #918:30min Please use {@link org.cactoos.scalar.Sticky} decorator
* class so that multiple calls to body() would produce the same cached result
* rather than throwing an exception.
*/
@EqualsAndHashCode(callSuper = true)
public final class RqOnce extends RqWrap {
Expand Down Expand Up @@ -64,5 +67,4 @@ private static Request wrap(final Request req) {
}
);
}

}
46 changes: 36 additions & 10 deletions src/main/java/org/takes/rs/ResponseOf.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,17 @@
*/
package org.takes.rs;

import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;
import org.cactoos.scalar.And;
import org.cactoos.scalar.HashCode;
import org.cactoos.scalar.Or;
import org.cactoos.scalar.Unchecked;
import org.takes.Response;
import org.takes.Scalar;
import org.takes.misc.InputStreamsEqual;

/**
* Response of head and body.
Expand All @@ -52,21 +59,17 @@ public final class ResponseOf implements Response {
* @param head Iterable head value
* @param body InputStream body value
*/
public ResponseOf(final Iterable<String> head,
final InputStream body) {
this(
() -> head,
() -> body
);
public ResponseOf(final Iterable<String> head, final InputStream body) {
this(() -> head, () -> body);
}

/**
* Ctor.
* @param head Scalar to provide head value
* @param body Scalar to provide body value
*/
public ResponseOf(final Scalar<Iterable<String>> head,
final Scalar<InputStream> body) {
public ResponseOf(
final Scalar<Iterable<String>> head, final Scalar<InputStream> body) {
this.shead = head;
this.sbody = body;
}
Expand All @@ -82,12 +85,35 @@ public InputStream body() throws IOException {
}

@Override
@SuppressFBWarnings("EQ_UNUSUAL")
public boolean equals(final Object that) {
return this == that || ResponseOf.class.equals(that.getClass());
return new Unchecked<>(
new Or(
() -> this == that,
new And(
() -> that != null,
() -> ResponseOf.class.equals(that.getClass()),
() -> {
final ResponseOf other = (ResponseOf) that;
return new And(
() -> {
final Iterator<String> iter = other.head()
.iterator();
return new And(
(String hdr) -> hdr.equals(iter.next()),
this.head()
).value();
},
new InputStreamsEqual(this.body(), other.body())
).value();
}
)
)
).value();
}

@Override
public int hashCode() {
return 0;
return new HashCode(new Unchecked<>(this.shead::get).value()).value();
}
}
Loading

0 comments on commit 39a6c72

Please sign in to comment.