6 Replies Latest reply: May 17, 2010 12:15 PM by 843793 RSS

    Generic class

    843793
      I have one inteface



      public interface Semiring<L>{

           public L join(L a);
           
           public L meet(L a);
           
           public L top();
           
           public L bot();
      }



      one abstract class

      abstract class Label<L extends Comparable<L>> implements Comparable<Label<L>>,Semiring<L>{

           private L value;
           
           /**
           * Set the value of Label <tt>value</tt>.
           *
           * @param value
           */
           public Label(L value){
                this.value = value;
           }
           
           /**
           * Get the value of Label <tt>value</tt>.
           *
           */
           public L getValue(){
                return value;
           }
           
           /**
           * Verify if Label <tt>label</tt> is equals to Label <tt>value</tt>.
           *
           * @param label
           */
           public boolean equals(Label<L> label){
                return value.equals(label.getValue());
           }
           
           
           /**
           * Compare Label <tt>value</tt> to Label <tt>label</tt>
           *
           * @param label
           */
           public int compareTo(Label<L> label) {
                return value.compareTo(label.getValue());
           }

           @Override
           public int hashCode() {
                final int prime = 31;
                int result = 1;
                result = prime * result + ((value == null) ? 0 : value.hashCode());
                return result;
           }

           /**
           * Verify if Label <tt>label</tt> is equals to Object <tt>obj</tt>.
           *
           * @param label
           */
           @Override
           public boolean equals(Object obj) {
                if (this == obj)
                     return true;
                if (obj == null)
                     return false;
                if (getClass() != obj.getClass())
                     return false;
                Label<?> other = (Label<?>) obj;
                if (value == null) {
                     if (other.value != null)
                          return false;
                } else if (!value.equals(other.value))
                     return false;
                return true;
           }
      }

      and 5 instances of Label
      public class LabelBoolean extends Label<Boolean>
      ....
      ....
      ....

      one class that use Label
      public class Entry<S extends Comparable<S>, T extends Comparable<T>, L extends Label<?>> implements Comparable<Entry<S, T, L>>

      Until here i dont have any problem, but with this class

      public class EntrySetLRelation<S extends Comparable<S>, T extends Comparable<T>, L extends Label<?>>
      extends AbstractLRelation<S, T ,L>


      i have one error

      public <U extends Comparable<U>> LRelation<S, U, L> compose(LRelation<T, U, L> relation) {
      /*if (relation instanceof DelayedTransitiveClosure) {
      return (LRelation<S, U, L>)this.compose(LRelations.multiSourceReachMark((LRelation<T, T, L>)relation, this.range()));
      }*/
      LRelation<S, U, L> result = new EntrySetLRelation<S, U, L>();
      for (Entry<S, T, L> e : set) {
      for (Entry<T, U, L> e2 : relation) {
      if (e.getSecond().equals(e2.getFirst())){
           
      result.add(new Entry<S, U, L>(e.getFirst(), e2.getSecond(), new L(e.getLabel().meet(e2.getLabel().getValue()))));
      }
      }
      }
      return result;
      }

      "
      The method meet(capture#4-of ?) in the type Semiring<capture#4-of ?> is not applicable for the
      arguments (capture#1-of ?)
      "
        • 1. Re: Generic class
          843793
          // Code tags are our friends. 
          • 2. Re: Generic class
            843793
            when i wrote i try
             and now i cant edit :(                                                                                                                                                                                                                
            • 3. Re: Generic class
              EJP
              So edit first then
                                                                                                                                                                                                                                                      
              • 4. Re: Generic class
                843793
                Post an SSCCE . Your examples are lacking critical information (i.e. missing types) to make it compilable so we can diagnose your problem.

                Edit: Meh I can see one glaring error with your implementation that will probably require a complete rethink of your design:
                new L( //...
                You can't instantiate a generic type, especially with a custom constructor. This is definitely a code smell, but if it's really required you'll have to look into Reflection.

                Edited by: endasil on 17-May-2010 9:31 AM
                • 5. Re: Generic class
                  843793
                  how can i ensures that Label Type is only Integer,Boolean,Double,FLoat or Complex??
                  • 6. Re: Generic class
                    843793
                    marciojc wrote:
                    how can i ensures that Label Type is only Integer,Boolean,Double,FLoat or Complex??
                    Why do you want to? You typically add restrictions because you rely on some part of a common interface (for instance, with comparable you need to know that the compareTo method will be there when you look for it). What are you trying to gain by needlessly restricting the Label type?