Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8215591
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T11:50:29+00:00 2026-06-07T11:50:29+00:00

I want to register a custom formatter for handling Set<Integer> . The obvious way:

  • 0

I want to register a custom formatter for handling Set<Integer>.

The obvious way:

Formatters.register(Set<Integer>.class, new AnnotationIntegerSetFormatter());

results in “Illegal start of expression”. What’s the right way to do this, or is it not possible?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T11:50:32+00:00Added an answer on June 7, 2026 at 11:50 am

    Ended up using Formatters.register(Set.class, new AnnotationIntegerSetFormatter());; parse can still return Set<Integer> and in print just cast the Set to Set<Integer>:

    public class AnnotationIntegerSetFormatter extends Formatters.AnnotationFormatter<IntegerSet,Set> {
        @Override
        public Set<Integer> parse(IntegerSet annotation, String text, Locale locale) {
            Set<Integer> set = new TreeSet<Integer>();
            for (String part : text.split(","))
                set.add(Integer.parseInt(part));
    
            return set;
        }
    
        @Override
        public String print(IntegerSet annotation, Set value, Locale locale) {
            List<Integer> sorted = new ArrayList<Integer>();
            sorted.addAll((Set<Integer>) value);
            Collections.sort(sorted);
    
            return join(",", sorted.toArray());
        }
    
        private static String join(String separator, Object... values) {
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < values.length; i++) {
                if (values[i] == null)
                    continue;
    
                if (sb.length() > 0)
                    sb.append(separator);
    
                sb.append(values[i].toString());
            }
    
            return sb.toString();
        }
    
    }
    

    and for completeness, the annotation I used:

    @Target(ElementType.FIELD)
    @Retention(RetentionPolicy.RUNTIME)
    public @interface IntegerSet {
        int min() default Integer.MIN_VALUE;
        int max() default Integer.MAX_VALUE;
    }
    

    Since this is annotation based, it appears you can register multiple formatters for a Collection type and have them distinguished by the annotations.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to fire event in custom class written in Extjs 4. I created
How to create custom login and register page in wordpress? I want to show
In MVC3 application I have my custom implementation of IPrincipal. I want to register
I want to register a script with wordpress that will work on the backend.
I want to register for screen resolution changes on the Mac. I have a
I have a win32 COM dll. I want to register this dll for different
I have created a COM Component in C++. Now i want to register my
NB: i´m using Twig in a non-Symfony context. I want to register a master
I have an animation running in the background, and I want to register a
In my web application i have registration form, when user register i want to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.