?
Solved

static analysis tool warning

Posted on 2011-10-13
8
Medium Priority
?
171 Views
Last Modified: 2012-05-12
I have the following class where as you see I need the third member variable to be non final
because I need to assign it a value later.

I run the static analysis tool ( compulsory here)

and it was complaining about Mutable Comparison , is there a way to get rid of this warning ?

Thanks
public static class Triplet<A, B, C> implements Comparable<Triplet>{
	    public final A first;
	    public final B second;
	    public C third;
	    
	    public Quintuplet(A first, B second, C third ) {
	        this.first = first;
	        this.second = second;
	        this.third = third;
	        
	    }

		public int compareTo(Triplet o) {
			if (!(first.equals(o.first))){
				return (((String)first).compareTo((String)o.first));
			}else if (!(second.equals(o.second))){
				return (((String)second).compareTo((String)o.second));
			}else if (!(third.equals(o.third))){					// MUTABLE COMPARISON
				return (((String)third).compareTo((String)o.third));		// MUTABLE COMPARISON

			}
		}
	}

Open in new window

0
Comment
Question by:bachra04
  • 6
  • 2
8 Comments
 
LVL 47

Expert Comment

by:for_yan
ID: 36965689
Make one more final variable - you can assign to it once - so yoiu assign third to it and compare with that variable.
Not very smart in my opinion - but there will not be warning I hope
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36965728

This at least should not complain and should work the same:
(ther were by the way some comippilation mistakes name of the construcvtor, no return vlaue - I fixed them).

public class Outer {
    public static class Triplet<A, B, C> implements Comparable<Triplet>{
	    public final A first;
	    public final B second;
	    public C third;
        public final C third1;
	    
	    public Triplet(A first, B second, C third ) {
	        this.first = first;
	        this.second = second;
	        this.third = third;
            this.third1 = third;
           
	        
	    }

		public int compareTo(Triplet o) {
			if (!(first.equals(o.first))){
				return (((String)first).compareTo((String)o.first));
			}else if (!(second.equals(o.second))){
				return (((String)second).compareTo((String)o.second));
			}else if (!(third1.equals(o.third1))){					// MUTABLE COMPARISON
				return (((String)third1).compareTo((String)o.third1));		// MUTABLE COMPARISON

			}
            return 0;
		}
	}
    
}

Open in new window

0
 
LVL 2

Author Comment

by:bachra04
ID: 36966194
I think there is a bug in the above code:

First Triplet("1", "2","5");
Second Triplet("1","2","5"); then call third = "7";

but still firstTriplet.compareTo(secondTriplet) return TRUE;
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 47

Expert Comment

by:for_yan
ID: 36966206
Please, post the full code - how do you execute it?
This is the inner class - I only chaecked that it compiles - there iis no main class - please post the full code which you execute.
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36966212
When I was checking I was only concerned with compilation of final line.

It was not compiling the compareTo method as it not always has a return value in your code,
so I added return 0 only to staisfy compiler.
That's why you are getting true now.

AAnyway oposet your fuil code and then I'll check


0
 
LVL 47

Expert Comment

by:for_yan
ID: 36966244
It actually runs correctly - see output below:

public class Outer {

    public static void main(String[] args) {
        Triplet<String,String,String> t1 = new Triplet<String,String,String>("1","2","5");
           Triplet<String,String,String> t2 = new Triplet<String,String,String>("1","2","7");
        System.out.println("t1 compare to t2: " +  t1.compareTo(t2));
     


    }

    public static class Triplet<A, B, C> implements Comparable<Triplet>{
	    public final A first;
	    public final B second;
	    public C third;
        public final C third1;

	    public Triplet(A first, B second, C third ) {
	        this.first = first;
	        this.second = second;
	        this.third = third;
            this.third1 = third;


	    }

		public int compareTo(Triplet o) {
			if (!(first.equals(o.first))){
				return (((String)first).compareTo((String)o.first));
			}else if (!(second.equals(o.second))){
				return (((String)second).compareTo((String)o.second));
			}else if (!(third1.equals(o.third1))){					// MUTABLE COMPARISON
				return (((String)third1).compareTo((String)o.third1));		// MUTABLE COMPARISON

			}
            return 0;
		}
	}

}

Open in new window


output:
t1 compare to t2: -2

Open in new window

0
 
LVL 2

Author Comment

by:bachra04
ID: 36968812
That's what i mean :

 
public class Outer {

    public static void main(String[] args) {
        Triplet<String,String,String> t1 = new Triplet<String,String,String>("1","2","5");
           Triplet<String,String,String> t2 = new Triplet<String,String,String>("1","2","5");
           t2.third = "7";
        System.out.println("t1 compare to t2: " +  t1.compareTo(t2));
     


    }

    public static class Triplet<A, B, C> implements Comparable<Triplet>{
	    public final A first;
	    public final B second;
	    public C third;
        public final C third1;

	    public Triplet(A first, B second, C third ) {
	        this.first = first;
	        this.second = second;
	        this.third = third;
            this.third1 = third;


	    }

		public int compareTo(Triplet o) {
			if (!(first.equals(o.first))){
				return (((String)first).compareTo((String)o.first));
			}else if (!(second.equals(o.second))){
				return (((String)second).compareTo((String)o.second));
			}else if (!(third1.equals(o.third1))){					// MUTABLE COMPARISON
				return (((String)third1).compareTo((String)o.third1));		// MUTABLE COMPARISON

			}
            return 0;
		}
	}

}

Open in new window


And the output is :

t1 compare to t2: 0

Open in new window

0
 
LVL 47

Accepted Solution

by:
for_yan earned 2000 total points
ID: 36969966
You know, thoe peeople who impose this static tool, etc, they want you to make this Triplet object immutable, so that you cannot change some part
of it - you need to create a new one instead - like with Strings.

So, I suggest just obey, if this is the requirement.
insted of doinig t2.third = "7", create new one with the thoird string "7".
Like in the code below:

public class Outer {

    public static void main(String[] args) {
        Triplet<String,String,String> t1 = new Triplet<String,String,String>("1","2","5");
           Triplet<String,String,String> t2 = new Triplet<String,String,String>(t1.getFirst(),t1.getSecond(),"7");
        System.out.println("t1 compare to t2: " +  t1.compareTo(t2));
        //   Triplet<String,String,String> t3 = new Triplet<String,String,String>("1","2","7");

     


    }

    public static class Triplet<A, B, C> implements Comparable<Triplet>{
	    public final A first;
	    public final B second;
	    public final C third;
       // public final C third1;

	    public Triplet(A first, B second, C third ) {
	        this.first = first;
	        this.second = second;
	        this.third = third;
            //this.third1 = third;


	    }

        public A getFirst(){return first;}
              public B getSecond(){return second;}
              public C getThird(){return third;}


		public int compareTo(Triplet o) {
			if (!(first.equals(o.first))){
				return (((String)first).compareTo((String)o.first));
			}else if (!(second.equals(o.second))){
				return (((String)second).compareTo((String)o.second));
			}else if (!(third.equals(o.third))){					// MUTABLE COMPARISON
				return (((String)third).compareTo((String)o.third));		// MUTABLE COMPARISON

			}
            return 0;
		}
	}

}

Open in new window


Output:
t1 compare to t2: -2

Open in new window

0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
The viewer will learn how to implement Singleton Design Pattern in Java.
Suggested Courses
Course of the Month15 days, 18 hours left to enroll

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question