/* * [Clue.java] * * Summary: Weighted String that indicates a product is in stock or out of stock. * * Copyright: (c) 2012-2017 Roedy Green, Canadian Mind Products, http://mindprod.com * * Licence: This software may be copied and used freely for any purpose but military. * http://mindprod.com/contact/nonmil.html * * Requires: JDK 1.8+ * * Created with: JetBrains IntelliJ IDEA IDE http://www.jetbrains.com/idea/ * * Version History: * 1.0 2012-03-01 initial version * 1.1 2016-09-06 remove synchronized */ package com.mindprod.stores; import com.mindprod.common18.ST; import com.mindprod.fastcat.FastCat; import org.jetbrains.annotations.NotNull; import java.util.regex.Pattern; import static java.lang.System.*; /** * Weighted String that indicates a product is in stock or out of stock. * * @author Roedy Green, Canadian Mind Products * @version 1.0 2012-03-01 initial version * @since 2012-03-01 */ class Clue implements Comparable { /** * what this marker means */ private final Hint hint; /** * case-sensitive String that indicates a product is either in stock or out of stock. */ private final Marker marker; /** * true if this clue conflicts with some other clue */ private boolean conflicted = false; /** * true if this clue ever matched */ private boolean used = false; /** * the effect on determining if this clue match had on carry on the last match */ private int carryEffect = 0; /** * Constructor * * @param hint what the marker means whene encountered * @param marker string to look, embedded xxx expanded to current isbn, or lead regex: */ Clue( final Hint hint, final String marker ) { this.hint = hint; if ( marker.startsWith( "regex:" ) ) { Pattern pattern = Pattern.compile( ST.chopLeadingString( marker, "regex:" ) ); this.marker = new MarkerRegex( pattern ); } else if ( marker.contains( "xxx" ) ) { this.marker = new MarkerProduct( marker ); // expand xxx later } else { this.marker = new MarkerString( marker ); } } static void debug() { Clue[] clues = EStore.NCIXUS.getClues(); FastCat sb = new FastCat( 5 ); sb.append( "used clues:" ); for ( Clue clue : clues ) { sb.append( clue.used ); } out.println( sb.toSpaceList() ); } /** * get the effect on determining if this clue match had on carry on the last match */ int getCarryEffect() { return this.carryEffect; } /** * Get Hint on what this marker string means.. * * @return String might find in store response. */ Hint getHint() { return hint; } /** * depending on whether this marker appears in the big string, what in the effect on the score of likelihood of * being is stock? * * @param big response from website, trimmed with head and tail cut off. * * @return inStock eight that this marker presence/absence contributes * @see #carryEffect */ int getInStockEffect( final String big ) { // works with simple strings, Regex and embedded isbn final boolean match = marker.contained( big ); final int instockEffect = match ? hint.getInStockWeightIfPresent() : hint.getInStockWeightIfAbsent(); final int carryEffect = match ? hint.getCarryWeightIfPresent() : hint.getCarryWeightIfAbsent(); // save so caller can pick it up later. this.carryEffect = carryEffect; return instockEffect; } /** * A case-sensitive String that indicates a product is either in stock or out of stock. * * @return String might find in store response. */ Marker getMarker() { return marker; } /** * find out offset where marker appears in big, -1 if never * * @return offset or -1 */ int indexAt( String big ) { // indexAt may be a regex or standard indexAt final int result = marker.indexAt( big ); if ( result >= 0 ) { markUsed(); } return result; } /** * was this clue conflicted with some other clues? * * @return true if conflicted */ boolean isConflicted() { return this.conflicted; } /** * was this clue used, matched? * * @return true if used */ boolean isUsed() { return this.used; } /** * note that this clue conflicts with some other clue */ void markConflicted() { this.conflicted = true; } /** * mark this Clue as used */ private void markUsed() { // out.println( getMarker().toString() + " used" ); this.used = true; } /** * record the product/isbn13 for embedded isbns in markers * * @param product product number, usually isbn13 we are probing */ void setProduct( final String product ) { marker.setProduct( product ); } /** * Describe/summarise the comparison here.. * Defines default the sort order for Clue Objects. * Compare this Clue with another Clue with JDK 1.5+ generics. * Compares marker case sensitively. * Informally, returns (this-other) or +ve if this sorts after other. * The Java source code for this Comparable implementation was generated by the * Canadian Mind Products ComparatorCutter Applet at http://mindprod.com/applet/comparatorcutter.html * * @param other other Clue to compare with this one * * @return +ve if this>other, 0 if this==other, -ve if this<other */ public final int compareTo( @NotNull Clue other ) { return Integer.compare( hint.ordinal(), other.hint.ordinal() ); // sort by Marker is stable, leave as it } }