Solved

Issues with casting

Posted on 2013-02-06
4
431 Views
Last Modified: 2013-02-06
My users can select data real time as long as the application is open.
So I am using a WritableList with a Listener to store user selected data.  Using the WritableList allows my Tables to be updated on the fly.  So when a user adds data to the List, the listener fires and updates my tables.  Works the same for when data is removed.

The Method I am using to add the user data to the WritableList

public void add(TCComponentItemRevision tcRevision, TCComponentDataset selectedDataset) {
      AplotDatasetData pp = new AplotDatasetData(tcRevision, selectedDataset);
      if (!observableList.contains(pp)) {
         observableList.add(pp);
      }
   }

Open in new window


I am using the Class AplotDatasetData to format the data to add to my List
   
   ////////////////////////////////////////////////////////////////////////   
   // Start of AplotDatasetData Class                                    // 
   ////////////////////////////////////////////////////////////////////////   
   public class AplotDatasetData {

      TCComponentItemRevision rev;
      TCComponentDataset   componentdataset;
      String markUp = "no";
              
      //////////////////////////////////////////////////////////////////////////
      //                           Constructor                                //
      //////////////////////////////////////////////////////////////////////////
      public AplotDatasetData(TCComponentItemRevision tcRevision, TCComponentDataset selectedDataset) {
         rev = tcRevision;
         componentdataset = selectedDataset;
                  
      }// end Constructor
      
      //////////////////////
      //   getDataset()   //
      //////////////////////
      public TCComponent getDataset() {
         return componentdataset;
      }
      
      //////////////////
      //   getRev()   //
      //////////////////
      public TCComponent getRev() {
         return rev;
      }

      //////////////////
      //   equals()   //
      //////////////////
      @Override
      public boolean equals(Object o) {
         AplotDatasetData p = (AplotDatasetData) o;
         if (rev.equals(p.getRev()) && componentdataset.equals(p.getDataset())) {
            return true;
         }
         else {
           return false;
          }
      }// end equals()
   }

Open in new window


The code complies.  Even when the user is adding data.  The code works great, checks for duplicate data being entered.  But when other operations are executed, it fails with the following error.

ERROR: 11:31:00,591 - TcLogger$IC_LogListener.logging:? org.eclipse.core.runtime - org.eclipse.ui - 0 - Unhandled event loop exception org.eclipse.swt.SWTException: Failed to execute runnable (java.lang.ClassCastExc eption: org.eclipse.core.databinding.observable.list.WritableList cannot be cast to com.lexmark.aplot.datamodels.AplotDataModel$AplotDatasetData)
etc .....
Caused by: java.lang.ClassCastException: org.eclipse.core.databinding.observable .list.WritableList cannot be cast to com.lexmark.aplot.datamodels.AplotDataModel $AplotDatasetData at com.lexmark.aplot.datamodels.AplotDataModel$AplotDatasetData.equals(A plotDataModel.java:167

So what I know.

I know it is a casting issue in the equals method.
AplotDatasetData p = (AplotDatasetData) o;

Open in new window


1.  I am not sure how to change the equals method?
2.  Am I not creating the WritableList correctly?
     There are other places in the code where I need to create a new ArrayList with the data    in the WritableList
ArrayList<AplotDataModel.AplotDatasetData> tableData = new ArrayList<AplotDataModel.AplotDatasetData>(AplotDataModel.getInstance().getObservableList());

Open in new window


The above could code could be where the equals() is being called and causing the casting error.

I am looking for any help
0
Comment
Question by:jkteater
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 86

Accepted Solution

by:
CEHJ earned 500 total points
ID: 38860397
if (!(o instanceof AplotDatasetData)) {
  return false;
} 

Open in new window

or something to that effect should probably start your equals method
0
 

Author Comment

by:jkteater
ID: 38860488
Thank you very much!!  The comment works perfectly.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 38860507
:) It must be said that you should not normally be using equals with different types. If you are, this could indicate an error
0
 

Author Comment

by:jkteater
ID: 38860578
This is the finished product.  

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

Open in new window


@Override
      public boolean equals(Object obj) {
         if (this == obj)
            return true;
         if (obj == null)
            return false;
         if (getClass() != obj.getClass())
            return false;
         AplotDatasetData other = (AplotDatasetData) obj;
         if (!getOuterType().equals(other.getOuterType()))
            return false;
         if (componentdataset == null) {
            if (other.componentdataset != null)
               return false;
         }
         else if (!componentdataset.equals(other.componentdataset))
            return false;
         if (rev == null) {
            if (other.rev != null)
               return false;
         }
         else if (!rev.equals(other.rev))
            return false;
         return true;
      }

      private AplotDataModel getOuterType() {
         return AplotDataModel.this;
      }

Open in new window

0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses

632 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