#736: KeyValueBean.java

projectforge-business/src/main/java/org/projectforge/framework/utils/KeyValueBean.java Lines: 82 · Author: Kai Reinhard · Type: Java generic POJO 82 lines · 40 code · 29 comments · 13 blank

Purpose

A generic key-value pair, similar to java.util.Map.Entry but standalone and comparable. Used everywhere in ProjectForge where two pieces of data need to travel together — dropdown options, configuration parameters, HTML tag attributes, sort criteria, chart labels, etc. 82 lines.

The class has two type parameters: KeyType (unconstrained) and ValueType extends Comparable<ValueType> (must be comparable — because the bean itself implements Comparable based on the value). This is different from ILabelValueBean — that interface doesn't require comparability; this class does, because it's used in sorted collections.

Comparable behavior

The compareTo() method compares by value only, ignoring the key entirely. The value must implement Comparable — enforced at the type level. This means two KeyValueBeans with different keys but equal values are considered equal for ordering purposes. This is correct for sorted dropdown lists (where you sort by the display text, not the ID), but surprising if you use KeyValueBean as a HashMap key (where the equals/hashCode matter more — and those are inherited from Object, not overridden). This is a deliberate design compromise: KeyValueBean is primarily for ordered display, not for map storage.

Constructors

Relationship to ILabelValueBean

Key-value vs label-value — same pattern, different semantics:

They're conceptually the same pattern but used in different contexts. The naming distinction (Label vs Key) was intentional — it helps readers understand the domain: label/value for UI, key/value for internal data structures.