com.slothpetrochemical.bridgeprob.BridgeProblemSignature.java Source code

Java tutorial

Introduction

Here is the source code for com.slothpetrochemical.bridgeprob.BridgeProblemSignature.java

Source

/*
 * Copyright (c) 2013. Released under MIT license, see LICENSE.txt.
 */

package com.slothpetrochemical.bridgeprob;

import org.apache.commons.lang3.StringUtils;

import java.util.Arrays;
import java.util.List;

/**
 * Class providing a compact representation of a single state in the
 * Bridge/Flashlight problem.
 */
public final class BridgeProblemSignature
        implements Comparable<BridgeProblemSignature>, BridgeProblemStateDescriptor {
    public static final String BRIDGE_DELIMITER = " || ";
    public static final String NAME_DELIMITER = ",";
    public static final String FLASHLIGHT = "(*)";

    private final String signature;

    BridgeProblemSignature(final BridgeProblemStateDescriptor descriptor) {
        if (descriptor instanceof BridgeProblemSignature) {
            BridgeProblemSignature otherSig = (BridgeProblemSignature) descriptor;
            this.signature = otherSig.signature;
        } else {
            this.signature = BridgeProblemSignature.composeSignature(descriptor);
        }
    }

    @Override
    public int compareTo(final BridgeProblemSignature o) {
        return this.signature.compareTo(o.signature);
    }

    // package private
    static String composeSignature(final BridgeProblemStateDescriptor descriptor) {
        StringBuilder sb = new StringBuilder();
        boolean flashlightOnLeft = descriptor.isFlashlightOnLeft();
        sb.append(StringUtils.join(descriptor.getNamesOnLeft(), NAME_DELIMITER));
        if (flashlightOnLeft) {
            sb.append(FLASHLIGHT);
        }
        sb.append(BRIDGE_DELIMITER);
        sb.append(StringUtils.join(descriptor.getNamesOnRight(), NAME_DELIMITER));
        if (!flashlightOnLeft) {
            sb.append(FLASHLIGHT);
        }
        return sb.toString();
    }

    private CharSequence getLeftSegment() {
        int bridgeIdx = this.signature.indexOf(BRIDGE_DELIMITER);
        return this.signature.subSequence(0, bridgeIdx);
    }

    private CharSequence getRightSegment() {
        int bridgeIdx = this.signature.indexOf(BRIDGE_DELIMITER);
        return this.signature.subSequence(bridgeIdx + BRIDGE_DELIMITER.length(), this.signature.length());
    }

    private static CharSequence stripFlashlight(final CharSequence seq) {
        int lastIdx = StringUtils.lastIndexOf(seq, FLASHLIGHT);
        return lastIdx >= 0 ? seq.subSequence(0, lastIdx) : seq;
    }

    @Override
    public List<String> getNamesOnLeft() {
        String names = stripFlashlight(this.getLeftSegment()).toString();
        return Arrays.asList(StringUtils.splitByWholeSeparator(names, NAME_DELIMITER));
    }

    @Override
    public List<String> getNamesOnRight() {
        String names = stripFlashlight(this.getRightSegment()).toString();
        return Arrays.asList(StringUtils.splitByWholeSeparator(names, NAME_DELIMITER));
    }

    @Override
    public boolean isFlashlightOnLeft() {
        CharSequence leftSegment = this.getLeftSegment();
        return StringUtils.lastIndexOf(leftSegment, FLASHLIGHT) > -1;
    }

    //CHECKSTYLE:OFF
    @Override
    public boolean equals(final Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }

        BridgeProblemSignature that = (BridgeProblemSignature) o;

        if (!signature.equals(that.signature)) {
            return false;
        }

        return true;
    }

    @Override
    public int hashCode() {
        int result = signature.hashCode();
        return result;
    }
    //CHECKSTYLE:ON

    @Override
    public String toString() {
        return this.signature;
    }
}