title: How to pass an object with an unknown type to a Class with an unknown type
body: <p>I am currently working on a homework assignment for a Java programming course I am taking.  I am not asking for an exact answer, but for some guidance.  </p>
  
  <p>The problem I'm working on is this:</p>
  
  <p>I have a filter class that implements a Filter interface.  This interface has just one method - <code>matches(T element)</code></p>
  
  <p>I have configured my filter method to check an Integer that is being passed in for prime-ness.</p>
  
  <p>There is also a decorator class that decorates a collection class to only display objects that pass the filter.  </p>
  
  <p>I'm having problems getting the contains(Object o) method to work correctly.</p>
  
  <p>Basically the <code>contains(Obj o)</code> method in the  <code>FilteredCollection</code> class should first check to see if the object passes the filter, and then if it does, call the undecorated <code>contains()</code> method on that object.  </p>
  
  <p>Assuming I want to be able to use this <code>FilteredCollection</code> class with many different types of filters, How can I determine what type of object is being passed in, and then be able to pass that object to the current Filter that's being implemented.</p>
  
  <p>Here is my <code>PrimeNumberFilter</code> Class:</p>
  
  <pre><code>public class PrimeNumberFilter implements Filter&lt;Integer&gt; {
  
  public boolean matches(Integer e) {
      int n = e.intValue();
      if (n != 2 &amp;&amp; n % 2 == 0) {
          return false;
      }
  
      for (int i = 3; i * i &lt;= n; i += 2) {
          if (n % i == 0) {
              return false;
          }
      }
      return true;
  }
  
  }
  </code></pre>
  
  <hr>
  
  <p>Then here is my shortened <code>FilteredCollection</code> Class:</p>
  
  <pre><code>class FilteredCollection&lt;T&gt; implements Collection&lt;T&gt; {
  
  Collection&lt;T&gt; fc;
  Filter&lt;T&gt; currentFilter;
  
  private FilteredCollection(Collection&lt;T&gt; coll, Filter&lt;T&gt; filter) {
  
      this.fc = coll;
      this.currentFilter = filter;
  
  }
  
  public static &lt;T&gt; FilteredCollection&lt;T&gt; decorate(Collection&lt;T&gt; coll,
      Filter&lt;T&gt; filter) {
  
      return new FilteredCollection&lt;T&gt;(coll, filter);
  }
  
  public boolean contains(Object o) {
  
      //What do I do here?
      return fc.contains(o);
  }
  </code></pre>
  
  <p>The object being passed in to the contains method has to pass the filter, in this case a <code>PrimeNumberFilter</code>.  </p>
  
  <p>The error I'm getting is it keeps wanting to cast the object to type T, and I know that this will never work because of erasure.  </p>
  
  <p>I've done a ton of research, and I've boiled it down to needing to use reflection.</p>
  
  <p>The only hint my instructor will give me is that object only has a few methods I can use, and I should use one of those.  </p>
  
  <p>Thanks for your help!</p>
  
  <p><strong>EDIT:</strong>  One of the requirements of the project is to NOT cast an object to T in any method.  So while these answers are great, I am not able to use any of them.</p>