001package org.unix4j.unix.grep; 002 003import org.unix4j.line.Line; 004import org.unix4j.util.StringUtil; 005 006/** 007 * Performs fixed string comparisons to decide whether the pattern matches a 008 * line. Member classes exist for different grep options. 009 */ 010abstract class FixedStringMatcher implements LineMatcher { 011 012 protected final String pattern; 013 014 public FixedStringMatcher(String pattern) { 015 this.pattern = pattern; 016 } 017 018 public static class Standard extends FixedStringMatcher { 019 public Standard(GrepArguments args) { 020 super(args.getRegexp()); 021 } 022 @Override 023 public boolean matches(Line line) { 024 return line.getContent().contains(pattern); 025 } 026 } 027 public static class IgnoreCase extends FixedStringMatcher { 028 public IgnoreCase(GrepArguments args) { 029 super(args.getRegexp()); 030 } 031 @Override 032 public boolean matches(Line line) { 033 return StringUtil.containsIgnoreCase(line.getContent(), pattern); 034 } 035 } 036 public static class WholeLine extends FixedStringMatcher { 037 public WholeLine(GrepArguments args) { 038 super(args.getRegexp()); 039 } 040 @Override 041 public boolean matches(Line line) { 042 return line.getContent().equals(pattern); 043 } 044 } 045 public static class WholeLineIgnoreCase extends FixedStringMatcher { 046 public WholeLineIgnoreCase(GrepArguments args) { 047 super(args.getRegexp()); 048 } 049 @Override 050 public boolean matches(Line line) { 051 return line.getContent().equalsIgnoreCase(pattern); 052 } 053 } 054 055}