All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.gs.obevo.util.hash.CompoundHashingStrategy Maven / Gradle / Ivy

The newest version!
/**
 * Copyright 2017 Goldman Sachs.
 * 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.gs.obevo.util.hash;

import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;
import org.eclipse.collections.api.block.HashingStrategy;
import org.eclipse.collections.api.block.function.Function;
import org.eclipse.collections.api.block.procedure.Procedure;
import org.eclipse.collections.api.list.ImmutableList;

class CompoundHashingStrategy implements HashingStrategy {
    private final ImmutableList> keyAttrs;

    CompoundHashingStrategy(ImmutableList> keyAttrs) {
        this.keyAttrs = (ImmutableList>) keyAttrs;
    }

    public int computeHashCode(final T object) {
        final HashCodeBuilder hash = new HashCodeBuilder(17, 37);
        this.keyAttrs.forEach(new Procedure>() {
            @Override
            public void value(Function attr) {
                hash.append(attr.valueOf(object));
            }
        });
        return hash.toHashCode();
    }

    public boolean equals(final T object1, final T object2) {
        final EqualsBuilder equals = new EqualsBuilder();
        this.keyAttrs.forEach(new Procedure>() {
            @Override
            public void value(Function attr) {
                equals.append(attr.valueOf(object1), attr.valueOf(object2));
            }
        });
        return equals.isEquals();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy