History | Log In     View a printable version of the current page.  
Issue Details (XML | Word | Printable)

Key: IDEADEV-2334
Type: New Feature New Feature
Status: Open Open
Priority: Major Major
Assignee: Alexey Kudravtsev
Reporter: Peter Lawrey
Votes: 0
Watchers: 0
Operations

If you were logged in you would be able to see more operations.
IDEA: Development

As well as the intension "Cast to String" offer "Call toString()"

Created: 31 Aug 05 13:45   Updated: 19 Sep 07 23:48
Component/s: Editor. Intention Actions
Fix Version/s: Diana Final

Original Estimate: Unknown Remaining Estimate: Unknown Time Spent: Unknown

Build: 3,461
Severity: Low


 Description  « Hide
I have been making more use of CharSequence in text processing to avoid unnecessary generating Strings.

A trivial example is.
public CharSequence cut(CharSequence str, int len) { if (str.length <= len) return; StringBuilder ret = new StringBuilder(len); ret.append(str.subSequence(0, len-3)); ret.append("..."); return ret; }

This result can be used in a StringBuilder, or be passed to a method which requires a String.
e.g.
public void setDescription(String description) { ... }

setDescription(cut(longDescription, MAX_LENGTH));

One of the options is to cast to String.

setDescription((String) cut(longDescription, MAX_LENGTH));

This may work some of the time, but a better, possiblily more reliable suggestion is call toString()

setDescription(cut(longDescription, MAX_LENGTH).toString());

I believe calling toString() may be a better way convert in a wide range of situations.



 All   Comments   Work Log   Change History      Sort Order:
Bas Leijdekkers - 31 Aug 05 16:03
but better watch out for NullPointerExceptions.

Peter Lawrey - 31 Aug 05 19:25
If you have fail fast coding and you want a NullPointerException as early as possible. .e.g as close to the caus eof the null value as possible.
In this case, a NullPointerException will be thrown earlier (e.g. str.length())

In cases where you want to preserve null values, you can keep the option to cast instead of calling toString().