/*
 * Copyright (C) 2005  Markus Kraetzig
 * Created on 16.09.2005
 */
package com.jstatcom.util;

import java.io.Serializable;

/**
 * A case insensitive nonempty string. It stores the original upper/lowercase
 * representation of a string but uses only the uppercase representation for
 * comparisons. This is useful to keep the original string representation for
 * rendering, for example for portfolio names, but make only case insensitive
 * lookups. Furthermore, all strings all trimmed.
 * 

* This class is immutable. * * @author Markus Kraetzig */ public final class CIString implements Comparable, Serializable { private final String origString; private final String upperCaseString; private final int hashCode; /** * Creates a portfolio string from a regular string. * * @param s * regular string * @throws IllegalArgumentException * if null argument or if s is * empty after trimming */ public CIString(String s) { if (s == null) throw new IllegalArgumentException("Argument was null."); s = s.trim(); if (s.length() == 0) throw new IllegalArgumentException( "Argument string was empty after trim."); this.origString = s; this.upperCaseString = s.toUpperCase(); this.hashCode = upperCaseString.hashCode(); } /** * Gets the original string. * * @return upper/lowercase string */ public String toString() { return origString; } /** * Gets the uppercase string. * * @return uppercase string representation */ public String toUpperCase() { return upperCaseString; } /** * Overwrite equals to compare only uppercase representation. * * @return true if o is PFString with same * uppercase representation, false otherwise */ public boolean equals(Object o) { if (!(o instanceof CIString)) return false; if (o == this) return true; return upperCaseString.equals(((CIString) o).upperCaseString); } /** * Overwritten because equals was overwritten. * * @return hashcode of uppercase representation */ public int hashCode() { return hashCode; } public int compareTo(CIString s) { return upperCaseString.compareTo(s.upperCaseString); } }

Ads help maintain this website.