/*
 * Copyright (C) 2012 - present by Yann Le Tallec.
 * Please see distribution for license.
 */
package com.assylias.jbloomberg;

import com.bloomberglp.blpapi.CorrelationID;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

/**
 * An immutable, thread safe class that holds a pair of CorrelationID and RealtimeField to uniquely identify
 * subscription data. The factory method provided guarantees uniqueness so that == and equals will always return the
 * same result.
 */
final class EventsKey {

    private final static ConcurrentMap<EventsKey, EventsKey> keys = new ConcurrentHashMap<>();
    private final CorrelationID id;
    private final RealtimeField field;
    private final int hash;

    /**
     * Factory method to ensure that there is only one EventsKey instance associated with a given correlation ID and a
     * field.
     */
    public static EventsKey of(CorrelationID id, RealtimeField field) {
        EventsKey key = new EventsKey(id, field);
        EventsKey previous = keys.putIfAbsent(key, key);
        return previous != null ? previous : key;
    }

    private EventsKey(CorrelationID id, RealtimeField field) {
        this.id = id;
        this.field = field;
        this.hash = getHashCode(id, field); //cached
    }

    @Override
    public int hashCode() {
        return hash;
    }

    /**
     * Assumes that obj is of the right type and is not null
     */
    @Override
    public boolean equals(Object obj) {
        final EventsKey other = (EventsKey) obj;
        if (!this.id.equals(other.id)) {
            return false;
        }
        if (this.field != other.field) {
            return false;
        }
        return true;
    }

    private int getHashCode(CorrelationID id, RealtimeField field) {
        int hash = 7;
        hash = 89 * hash + id.hashCode();
        hash = 89 * hash + field.hashCode();
        return hash;
    }

    @Override
    public String toString() {
        return "key [id=" + id + ", field=" + field + ", hash=" + hash + "]";
    }
}

/*
 * Copyright (C) 2012 - present by Yann Le Tallec.
 * Please see distribution for license.
 */
package com.assylias.jbloomberg;

import com.bloomberglp.blpapi.CorrelationID;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.function.Function;

/**
 * An immutable, thread safe class that holds a pair of CorrelationID and RealtimeField to uniquely identify
 * subscription data. The factory method provided guarantees uniqueness so that == and equals will always return the
 * same result.
 */
final class EventsKey {

    private final static ConcurrentMap<EventsKey, EventsKey> keys = new ConcurrentHashMap<>();
    private final CorrelationID id;
    private final RealtimeField field;
    private final int hash;

    /**
     * Factory method to ensure that there is only one EventsKey instance associated with a given correlation ID and a
     * field.
     */
    public static EventsKey of(CorrelationID id, RealtimeField field) {
        return keys.computeIfAbsent(new EventsKey(id, field), Function.identity());
    }

    private EventsKey(CorrelationID id, RealtimeField field) {
        this.id = id;
        this.field = field;
        this.hash = getHashCode(id, field); //cached
    }

    @Override
    public int hashCode() {
        return hash;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) return false;
        if (getClass() != obj.getClass()) return false;
        final EventsKey other = (EventsKey) obj;
        return Objects.equals(this.id, other.id) && this.field == other.field;
    }

    private int getHashCode(CorrelationID id, RealtimeField field) {
        return Objects.hash(id, field);
    }

    @Override
    public String toString() {
        return "key [id=" + id + ", field=" + field + ", hash=" + hash + "]";
    }
}

Simplify code in EventsKey