Title: RE: [Lang] Omitting a field in ToStringBuilder

Well, I also need something that /replaces/ values with, say, "<omitted>" for certain fields but still tells me when this field is null. I use this when I want to display a given field, I want to know when it is null and I want to know that it has a value but that value is not something I want in the String (for misc reasons).

Please see my attached current hack.

Thanks,
Gary

-----Original Message-----
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]]
Sent: Friday, February 14, 2003 2:30 AM
To: [EMAIL PROTECTED]
Subject: Re: [Lang] Omitting a field in ToStringBuilder


>  from:    Gary Gregory <[EMAIL PROTECTED]>
> I need to exclude a given field with
> ToStringBuilder.reflectionToString(Object).
> The Object I pass in is a javax.jms.Message which is a JMS abstract
> interface which can be implemented with all sorts of fields. I know I just
> do not want "messageText".
>
> My current implementation is nasty and does not feel right since I have to
> use a ToStringStyle:
>
>       private Object getMessageDescription(Message message) {
>               class TempToStringStyle extends ToStringStyle {
>                       public void append(StringBuffer buffer, String
> fieldName, Object value, Boolean fullDetail) {
>                               if ("messageText".equals(fieldName) ==
> false) {
>                     super.append(buffer, fieldName, value, fullDetail);
>                               }
>                       }
>               }
>               return ToStringBuilder.reflectionToString(message, new
> TempToStringStyle(), false);
>       }
>
>
> I could imagine doing something like:
>
> ToStringBuilder builder = new ToStringBuilder(message);
> builder.remove(messageText);
> return builder.toString();

An interesting requirement. This last solution is incomplete as it doesn't use reflectionToString. A ToStringStyle can be passed in to reflectionToString though, so your basic solution seems sound.

Perhaps what is needed is a ToStringStyle decorator (like the collections unmodifiable and synchronized decorators) that blocks certain field names? Essentially this would add your temporary tostringstyle to [lang].

Stephen




---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

 

        private class FieldFilterToStringStyle extends ToStringStyle {

                private List fieldNameList;

                public FieldFilterToStringStyle(List excludeList) {
                        this.setFieldNameList(excludeList);
                }

                public void append(StringBuffer buffer, String fieldName, Object 
value, Boolean fullDetail) {
                        if (this.accept(fieldName, value) == false) {
                                super.append(buffer, fieldName, value, fullDetail);
                        }
                }
        
                public boolean accept(String fieldName, Object value) {
                        return value != null && 
this.getFieldNameList().contains(fieldName);
                }

                public List getFieldNameList() {
                        return this.fieldNameList;
                }

                public void setFieldNameList(List fieldNameList) {
                        this.fieldNameList = fieldNameList;
                }
        }

        private class ExculeFieldsToStringStyle extends FieldFilterToStringStyle {

                public ExculeFieldsToStringStyle(List excludeList) {
                        super(excludeList);
                }

                public boolean accept(String fieldName, Object value) {
                        return !super.accept(fieldName, value);
                }
        }

        private class OmitValuesToStringStyle extends FieldFilterToStringStyle {

                private static final String DEFAULT_VALUE = "<omitted>";
                private String displayValue = DEFAULT_VALUE;

        public OmitValuesToStringStyle(List list, String displayValue) {
            super(list);
            this.setDisplayValue(displayValue);
        }

        public OmitValuesToStringStyle(List list) {
            this(list, DEFAULT_VALUE);
        }

                public void append(StringBuffer buffer, String fieldName, Object 
value, Boolean fullDetail) {
                        if (this.accept(fieldName, value)) {
                                value = DEFAULT_VALUE;
                        }
                        super.append(buffer, fieldName, value, fullDetail);
                }

                public String getDisplayValue() {
                        return this.displayValue;
                }

                public void setDisplayValue(String displayValue) {
                        this.displayValue = displayValue;
                }
        }

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to