-
Notifications
You must be signed in to change notification settings - Fork 184
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
…1525) Motivation: [Issue-1455](#1455) It is pretty trivial for a user to specify their own codec with a similar name, therefore it would be better to support equals/hashCode to allow for better equality checks. Modifications: - Implemented `equals` and `hashCode` on `IdentityContent`; - Changed all equality checks that were done using == and != to `equals`; - Add tests to verify new behavior; Result: `IdentityContentCodec` now implements `equals` and `hashCode`, and all equality checks are done using `equals()`.
- Loading branch information
Showing
16 changed files
with
220 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
...lk-encoding-api/src/test/java/io/servicetalk/encoding/api/CustomIdentityContentCodec.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/* | ||
* Copyright © 2021 Apple Inc. and the ServiceTalk project authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package io.servicetalk.encoding.api; | ||
|
||
/** | ||
* Extension of {@link NoopContentCodec} with custom implementations for {@link #equals(Object)} and {@link #hashCode()} | ||
* based on {@link #name()}. | ||
* This class can be used as is or can be extended to create a {@link ContentCodec} with custom behaviour. | ||
*/ | ||
class CustomIdentityContentCodec extends NoopContentCodec { | ||
|
||
CustomIdentityContentCodec() { | ||
super(Identity.identity().name()); | ||
} | ||
} |
73 changes: 73 additions & 0 deletions
73
...talk-encoding-api/src/test/java/io/servicetalk/encoding/api/IdentityContentCodecTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
/* | ||
* Copyright © 2021 Apple Inc. and the ServiceTalk project authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package io.servicetalk.encoding.api; | ||
|
||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.Arguments; | ||
import org.junit.jupiter.params.provider.MethodSource; | ||
|
||
import java.util.stream.Stream; | ||
|
||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.Matchers.equalTo; | ||
import static org.hamcrest.Matchers.is; | ||
import static org.hamcrest.Matchers.not; | ||
|
||
class IdentityContentCodecTest { | ||
|
||
private static final IdentityContentCodec IDENTITY_CODEC = new IdentityContentCodec(); | ||
|
||
private static Stream<Arguments> equalsParams() { | ||
return Stream.of( | ||
Arguments.of(IDENTITY_CODEC, new IdentityContentCodec()), | ||
Arguments.of(IDENTITY_CODEC, new CustomIdentityContentCodec()), | ||
Arguments.of(IDENTITY_CODEC, new CustomIdentityContentCodec() { | ||
@Override | ||
public int hashCode() { | ||
return IDENTITY_CODEC.hashCode() + 1; | ||
} | ||
|
||
@SuppressWarnings("RedundantMethodOverride") | ||
@Override | ||
public boolean equals(Object other) { | ||
return super.equals(other); | ||
} | ||
}), | ||
Arguments.of(IDENTITY_CODEC, new NoopContentCodec(IDENTITY_CODEC.name().toString().toUpperCase())) | ||
); | ||
} | ||
|
||
private static Stream<Arguments> notEqualsParams() { | ||
return Stream.of( | ||
Arguments.of(IDENTITY_CODEC, new Object()), | ||
Arguments.of(IDENTITY_CODEC, new NoopContentCodec(IDENTITY_CODEC.name() + "different")), | ||
Arguments.of(IDENTITY_CODEC, null) | ||
); | ||
} | ||
|
||
@ParameterizedTest(name = "{displayName} [{index}] {arguments}") | ||
@MethodSource("equalsParams") | ||
void equals(IdentityContentCodec identity, Object other) { | ||
assertThat(identity, is(equalTo(other))); | ||
} | ||
|
||
@ParameterizedTest(name = "{displayName} [{index}] {arguments}") | ||
@MethodSource("notEqualsParams") | ||
void notEquals(IdentityContentCodec identity, Object other) { | ||
assertThat(identity, is(not(equalTo(other)))); | ||
} | ||
} |
68 changes: 68 additions & 0 deletions
68
servicetalk-encoding-api/src/test/java/io/servicetalk/encoding/api/NoopContentCodec.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/* | ||
* Copyright © 2021 Apple Inc. and the ServiceTalk project authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package io.servicetalk.encoding.api; | ||
|
||
import io.servicetalk.buffer.api.Buffer; | ||
import io.servicetalk.buffer.api.BufferAllocator; | ||
import io.servicetalk.concurrent.api.Publisher; | ||
|
||
/** | ||
* Implementation of {@link ContentCodec} that doesn't modify the source {@link Buffer}. | ||
*/ | ||
class NoopContentCodec implements ContentCodec { | ||
|
||
private final CharSequence name; | ||
|
||
NoopContentCodec(CharSequence name) { | ||
this.name = name; | ||
} | ||
|
||
@Override | ||
public CharSequence name() { | ||
return name; | ||
} | ||
|
||
@Override | ||
public Buffer encode(Buffer src, BufferAllocator allocator) { | ||
return src; | ||
} | ||
|
||
@Override | ||
public Buffer encode(Buffer src, int offset, int length, BufferAllocator allocator) { | ||
return src; | ||
} | ||
|
||
@Override | ||
public Buffer decode(Buffer src, BufferAllocator allocator) { | ||
return src; | ||
} | ||
|
||
@Override | ||
public Buffer decode(Buffer src, int offset, int length, BufferAllocator allocator) { | ||
return src; | ||
} | ||
|
||
@Override | ||
public Publisher<Buffer> encode(Publisher<Buffer> from, BufferAllocator allocator) { | ||
return from; | ||
} | ||
|
||
@Override | ||
public Publisher<Buffer> decode(Publisher<Buffer> from, BufferAllocator allocator) { | ||
return from; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.