001package net.bramp.ffmpeg.info;
002
003import java.util.*;
004import net.bramp.ffmpeg.shared.CodecType;
005import org.apache.commons.lang3.builder.EqualsBuilder;
006import org.apache.commons.lang3.builder.HashCodeBuilder;
007
008public class FilterPattern {
009  /**
010   * Indicates whether this pattern represents a source or a sink and therefore has no other options
011   */
012  private final boolean sinkOrSource;
013
014  /** Indicates whether this pattern accepts a variable number of streams */
015  private final boolean variableStreams;
016
017  /** Contains a pattern matching the stream types supported */
018  private final List<CodecType> streams;
019
020  public FilterPattern(String pattern) {
021    this.sinkOrSource = pattern.contains("|");
022    this.variableStreams = pattern.contains("N");
023    List<CodecType> streams = new ArrayList<>();
024
025    for (int i = 0; i < pattern.length(); i++) {
026      final char c = pattern.charAt(i);
027
028      if (c == '|' || c == 'N') {
029        // These symbols are handled separately
030        continue;
031      }
032      if (c == 'A') {
033        streams.add(CodecType.AUDIO);
034      } else if (c == 'V') {
035        streams.add(CodecType.VIDEO);
036      } else {
037        throw new IllegalStateException("Unsupported character in filter pattern " + c);
038      }
039    }
040
041    this.streams = Collections.unmodifiableList(streams);
042  }
043
044  public boolean isSinkOrSource() {
045    return sinkOrSource;
046  }
047
048  public boolean isVariableStreams() {
049    return variableStreams;
050  }
051
052  public List<CodecType> getStreams() {
053    return streams;
054  }
055
056  @Override
057  public boolean equals(Object obj) {
058    return EqualsBuilder.reflectionEquals(this, obj);
059  }
060
061  @Override
062  public String toString() {
063    if (isSinkOrSource()) {
064      return "|";
065    }
066
067    if (isVariableStreams()) {
068      return "N";
069    }
070
071    return Arrays.toString(this.streams.toArray());
072  }
073
074  @Override
075  public int hashCode() {
076    return HashCodeBuilder.reflectionHashCode(this);
077  }
078}