package com.jexpect;

import static com.jexpect.Exceptions.throwNewIllegalArgumentExceptionForNot;

class ExpectToBeString implements ToBeString {

  private final StringMatcher matcher;

  ExpectToBeString(StringMatcher matcher) {
    this.matcher = matcher;
  }

  @Override
  public void toBe(String expected) {
    if (matcher.isNotEqualTo(expected)) {
      throw new IllegalArgumentException(String.format("Expected to be <%s>, but found: <%s>", expected, matcher.getActual()));
    }
  }

  @Override
  public void toBeNull() {
    if (matcher.isNotNull()) {
      throw new IllegalArgumentException(String.format("Expected to be <%s>, but found: <%s>", "null", matcher.getActual()));
    }
  }

  @Override
  public void toBeEmpty() {
    if (matcher.isNotEmpty()) {
      throw new IllegalArgumentException(String.format("Expected to be <%s>, but found: <%s>", "", matcher.getActual()));
    }
  }

  @Override
  public ToBeString not() {
    return new ExpectToBeStringNot(matcher);
  }

  private static class ExpectToBeStringNot implements ToBeString {

    private final StringMatcher matcher;

    private ExpectToBeStringNot(StringMatcher matcher) {
      this.matcher = matcher;
    }

    @Override
    public void toBeNull() {
      if (matcher.isNull()) {
        throwNewIllegalArgumentExceptionForNot(null, null);
      }
    }

    @Override
    public void toBeEmpty() {
      if (matcher.isEmpty()) {
        throwNewIllegalArgumentExceptionForNot("", "");
      }
    }

    @Override
    public void toBe(String expected) {
      if (matcher.isEqualTo(expected)) {
        throwNewIllegalArgumentExceptionForNot(expected, matcher.getActual());
      }
    }

    @Override
    public ToBeString not() {
      return new ExpectToBeString(matcher);
    }
  }
}

@@ -15,16 +15,87 @@
 xpect;%0A%0A
+import static com.jexpect.Exceptions.throwNewIllegalArgumentException;%0A
 import s
@@ -424,37 +424,35 @@
 )) %7B%0A      throw
- n
+N
 ew
- 
 IllegalArgumentE
@@ -464,62 +464,8 @@
 ion(
-String.format(%22Expected to be %3C%25s%3E, but found: %3C%25s%3E%22, 
 expe
@@ -482,33 +482,32 @@
 her.getActual())
-)
 ;%0A    %7D%0A  %7D%0A%0A  @
@@ -576,37 +576,35 @@
 )) %7B%0A      throw
- n
+N
 ew
- 
 IllegalArgumentE
@@ -616,62 +616,8 @@
 ion(
-String.format(%22Expected to be %3C%25s%3E, but found: %3C%25s%3E%22, 
 %22nul
@@ -632,33 +632,32 @@
 her.getActual())
-)
 ;%0A    %7D%0A  %7D%0A%0A  @
@@ -736,21 +736,19 @@
    throw
- n
+N
 ew
- 
 IllegalA
@@ -768,62 +768,8 @@
 ion(
-String.format(%22Expected to be %3C%25s%3E, but found: %3C%25s%3E%22, 
 %22%22, 
@@ -788,17 +788,16 @@
 ctual())
-)
 ;%0A    %7D%0A

remove duplicate code