1 Replies - 1606 Views - Last Post: 28 June 2018 - 01:42 AM

#1 wim81   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 07-June 18

Not shure how i have to implement the methods of Comparable class

Posted 28 June 2018 - 12:44 AM

Hi everyone,

i have a classcastException on my code and i know i have to implement the Comparable class.
But i don't know just how it works and to implement it.

My code so far for my entity class Project is:

@Entity
@NamedQueries({
        @NamedQuery(name = Project.QUERY_FIND_ALL_PROJECTS,
                query = "select p from Project p")
})
@Table(name="PROJECT")
public class Project implements Comparable<Project> {

    public static final String QUERY_FIND_ALL_PROJECTS = "project.findAllProjects";

    @Id
    @Column(name="ID")
    private Long id;

    @Column(name="NAME_NL")
    private String nameNl;

    @Column(name="NAME_FR")
    private String nameFr;

    @Column(name="VERSION")
    private Integer version;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getNameNl() {
        return nameNl;
    }

    public void setNameNl(String nameNl) {
        this.nameNl = nameNl;
    }

    public String getNameFr() {
        return nameFr;
    }

    public void setNameFr(String nameFr) {
        this.nameFr = nameFr;
    }

    public Integer getVersion() {
        return version;
    }

    public void setVersion(Integer version) {
        this.version = version;
    }

    public int compareTo(Project compareProject) {
            return;
    }

        @Override
    public String toString() {

        return;
    }




So i have already got the methods compareTo and toString()


The ClassCastException occurs in the following part:

 for(Project p: projects)
        {
            projectsMap.put(p,0.00);
        }




My code in the bean is:
@Named
@SessionScoped
public class ProjectHoursStatisticsBean implements Serializable {


    @Inject
    DayDataTaskService dayDataTaskService;

    @Inject
    ProjectTaskService projectTaskService;

    DayData data = new DayData();

    private List<DayData> dayDataList = new ArrayList<>();
    private List<Project> projects = new ArrayList<>();
    private Project project, projectAm, projectPm;
    private String name, projectName;
    private Month month;
    private Integer year;
    private Double days = 0.00;
    private Double projectDays = 0.00;
    LocalDate date = LocalDate.now().withDayOfMonth(1);
   TreeMap<Project, Double> projectsMap = new TreeMap<Project, Double>();
    public void onPageLoad() {
        try {
            if (FacesContext.getCurrentInstance().getPartialViewContext().isAjaxRequest()) {
                return;
            }


            month = date.getMonth();
            year = date.getYear();
            project = new Project();

            loadStatsList(month, year);


        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void loadStatsList(Month month, Integer year) {

        date = date.withMonth(month.getValue());
        date = date.withYear(year);
        LocalDate end = date.plusMonths(1);
        projects = projectTaskService.findAllProjects();
        for(Project p: projects)
        {
            projectsMap.put(p,0.00);
        }
        while(date.isBefore(end))
        {
            data = dayDataTaskService.findAllDataForUser("Wim", Date.valueOf(date));
           if(data != null) {

                name = data.getName();
               project = projectTaskService.findProjectById(Long.parseLong(data.getProjectAm()));
               projectsMap.put(project, projectsMap.containsKey(project) ? projectsMap.get(project) + 0.50 : 0.50);
              // projectsMap.computeIfPresent(project, projectsMap.get(project) + 0.50);

               }

           }
            date = date.plusDays(1);



        }



    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Month getMonth() {
        return month;
    }

    public void setMonth(Month month) {
        this.month = month;
    }

    public Double getProjectDays() {
        return projectDays;
    }

    public void setProjectDays(Double projectDays) {
        this.projectDays = projectDays;
    }

    public Map<Project, Double> getProjectsMap() {
        return projectsMap;
    }

    public void setProjectsMap(TreeMap<Project, Double> projectsMap) {
        this.projectsMap = projectsMap;
    }
}





Can anyone help and get me some more information on how the comparable class works.
I have read the java Comparable class documentation, but that's not so clear to me.

Thank you.

Wim

Is This A Good Question/Topic? 0
  • +

Replies To: Not shure how i have to implement the methods of Comparable class

#2 g00se   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3617
  • View blog
  • Posts: 16,596
  • Joined: 20-September 08

Re: Not shure how i have to implement the methods of Comparable class

Posted 28 June 2018 - 01:42 AM

What you're saying doesn't add up.

That can't be the source for Project as it wouldn't compile and you must have a version that does compile or you couldn't get runtime errors.
Please post the code you're using or you're just wasting everyone's time
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1