Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Arg#hashCode does not allocate #954

Merged
merged 2 commits into from
Jan 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions changelog/@unreleased/pr-954.v2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
type: improvement
improvement:
description: Arg#hashCode does not allocate
links:
- https://github.com/palantir/safe-logging/pull/954
7 changes: 7 additions & 0 deletions safe-logging/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,10 @@ apply plugin: 'com.palantir.revapi'
tasks.withType(JavaCompile) {
options.errorprone.disable 'PreferSafeLoggingPreconditions'
}

dependencies {
compileOnly 'com.google.code.findbugs:jsr305'

testImplementation 'junit:junit'
testImplementation 'org.assertj:assertj-core'
}
6 changes: 5 additions & 1 deletion safe-logging/src/main/java/com/palantir/logsafe/Arg.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ public final boolean equals(Object other) {

@Override
public final int hashCode() {
return Objects.hash(name, value);
int result = 1;
result = 31 * result + name.hashCode();
result = 31 * result + Objects.hashCode(value);
result = 31 * result + Boolean.hashCode(isSafeForLogging());
return result;
}
}
49 changes: 49 additions & 0 deletions safe-logging/src/test/java/com/palantir/logsafe/ArgTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* (c) Copyright 2024 Palantir Technologies Inc. All rights reserved.
*
* 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 com.palantir.logsafe;

import static org.assertj.core.api.Assertions.assertThat;

import java.util.Map;
import org.junit.Test;

public class ArgTest {

@Test
@SuppressWarnings({"EqualsWithItself", "AssertBetweenInconvertibleTypes"}) // testing equals
public void testSafeVersusUnsafe() {
SafeArg<String> safeArg = SafeArg.of("test", "value");
SafeArg<String> safeArg2 = SafeArg.of("test", "value");
UnsafeArg<String> unsafeArg = UnsafeArg.of("test", "value");

assertThat(safeArg.getName()).isEqualTo(unsafeArg.getName());
assertThat(safeArg.getValue()).isEqualTo(unsafeArg.getValue());
assertThat(safeArg.isSafeForLogging()).isTrue();
assertThat(safeArg2.isSafeForLogging()).isTrue();
assertThat(unsafeArg.isSafeForLogging()).isFalse();
assertThat(safeArg)
.isEqualTo(safeArg2)
.isEqualTo(safeArg)
.hasSameHashCodeAs(safeArg2)
.hasToString(safeArg2.toString())
.isNotEqualTo(Map.of("test", "value"));
assertThat(safeArg2).isEqualTo(safeArg).isEqualTo(safeArg2).isNotEqualTo(Map.of("test", "value"));
assertThat(unsafeArg).isNotEqualTo(safeArg).isNotEqualTo(safeArg2).isNotEqualTo(Map.of("test", "value"));
assertThat(safeArg).isNotEqualTo(unsafeArg).doesNotHaveSameHashCodeAs(unsafeArg);
assertThat(safeArg).hasToString("value").hasToString(unsafeArg.toString());
}
}