/* * [Marker.java] * * Summary: Interface for search by string or regex. * * Copyright: (c) 2014-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 2014-10-28 initial version */ package com.mindprod.stores; import java.util.regex.Matcher; import java.util.regex.Pattern; /** * Interface for search by string or regex. * * @author Roedy Green, Canadian Mind Products * @version 1.0 2014-10-28 initial version * @since 2012-03-01 */ abstract class Marker { String product; /** * is this marker contained in big? * true if marker is contained in big */ abstract boolean contained( String big ); /** * get the raw marker * * @return string search for as string */ abstract String getRawMarker(); /** * where is this marker contained in Big? -1 if nowhere * * @return int offset */ abstract int indexAt( String big ); /** * set the product number, usually isbn13 * * @param product */ void setProduct( final String product ) { this.product = product; } } class MarkerString extends Marker { private final String marker; /** * constructor */ MarkerString( String marker ) { this.marker = marker; } /** * is this marker contained in Big? */ public boolean contained( String big ) { return big.contains( marker ); } public String getRawMarker() { return marker; } /** * where is this marker contained in Big? -1 if nowhere */ public int indexAt( String big ) { return big.indexOf( marker ); } public String toString() { return marker; } } class MarkerProduct extends Marker { // must also match embedded ISBN private final String marker; /** * constructor */ MarkerProduct( String marker ) { this.marker = marker; } /** * is this marker contained in Big? */ public boolean contained( final String big ) { // replace all xxx final String seek = marker.replace( "xxx", this.product ); return big.contains( seek ); } public String getRawMarker() { return marker; } /** * where is this marker contained in Big? -1 if nowhere */ public int indexAt( final String big ) { // replace all xxx final String seek = marker.replace( "xxx", product ); return big.indexOf( seek ); } public String toString() { return marker; } } class MarkerRegex extends Marker { /** * store cooked Pattern as well as raw Marker */ private final Pattern pattern; /** * constructor */ MarkerRegex( Pattern pattern ) { this.pattern = pattern; } /** * is this marker contained in Big? */ public boolean contained( String big ) { final Matcher m = pattern.matcher( big ); return m.find(); } public String getRawMarker() { return pattern.toString(); } /** * where is this marker contained in Big? -1 if nowhere */ public int indexAt( String big ) { final Matcher m = pattern.matcher( big ); return m.find() ? m.start() : -1; } public String toString() { return "regex:" + pattern.toString(); } }