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 8342479
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T05:47:11+00:00 2026-06-09T05:47:11+00:00

I would like to trim down my code to use only one function for

  • 0

I would like to trim down my code to use only one function for filtering.
Filtering should work with 3 different classes: com.example.model.Category, com.example.model.Tag, java.util.Calendar.

Here’s my Controller

@Controller
@RequestMapping( "/blog" )
public class BlogController
{
    @Autowired
    private ArticleService articleService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private TagService tagService;

    private static final int PER_PAGE = 5;

    private static final int ARCHIVE_MONTHS = 12;

    @ModelAttribute
    public void addGlobalObjects( Map<String, Object> map )
    {
        map.put( "section", "blog" );

        SortedMap<Category, Integer> categories = new TreeMap<Category, Integer>();
        for ( Category category : categoryService.list() )
        {
            categories.put( category, articleService.size( category, Category.class ) );
        }

        Calendar cal = Calendar.getInstance();
        cal.set( Calendar.DAY_OF_MONTH, 1 );

        cal.add( Calendar.MONTH, ARCHIVE_MONTHS * -1 );

        SortedMap<Date, Integer> archive = new TreeMap<Date, Integer>();
        for ( int i = 0; i < ARCHIVE_MONTHS; ++i )
        {
            cal.add( Calendar.MONTH, 1 );
            archive.put( cal.getTime(), articleService.size( cal, Calendar.class ) );
        }

        SortedMap<Tag, Integer> tags = new TreeMap<Tag, Integer>();
        for ( Tag tag : tagService.list() )
        {
            tags.put( tag, articleService.size( tag, Tag.class ) );
        }

        map.put( "categories", categories );
        map.put( "archive", archive );
        map.put( "tags", tags );

        map.put( "categoriesSize", categoryService.size() );
        map.put( "tagsSize", tagService.size() );

        map.put( "date", new Date() );
    }

    @RequestMapping( "/index.html" )
    public String index( HttpServletRequest request, Map<String, Object> map )
    {
        return list( request, map, null, null );
    }

    @RequestMapping( "/archive/{date}.html" )
    public String archive( @PathVariable( "date" ) @DateTimeFormat( iso = ISO.DATE, style = "yyyy/MM" ) Date date, HttpServletRequest request, Map<String, Object> map )
    {
        Calendar cal = Calendar.getInstance();
        cal.setTime( date );

        return list( request, map, cal, Calendar.class );
    }

    private <T> String list( HttpServletRequest request, Map<String, Object> map, Object filterObject, Class<T> clazz )
    {
        int page = ServletRequestUtils.getIntParameter( request, "page", 1 );
        map.put( "articles", articleService.list( page * PER_PAGE - PER_PAGE, PER_PAGE, filterObject, clazz ) );
        map.put( "numPages", Math.ceil( articleService.size() / PER_PAGE ) );
        map.put( "currentPage", page );
        return "articles";
    }
}

In my ArticleDAO now I need to implement the methods list/size:

@Repository
public class ArticleDAO extends BaseDAO<Article>
{
    public <T> List<Article> list( int offset, int limit, Object filterObject, Class<T> clazz )
    {
        Criteria c = doFilter( filterObject, clazz );

        if ( limit > 0 )
        {
            c.setFirstResult( offset );
            c.setMaxResults( limit );
        }

        return c.list();
    }

    public <T> Integer size( Object filterObject, Class<T> clazz )
    {
        Number ret = ( Number ) doFilter( filterObject, clazz ).setProjection( Projections.rowCount() ).uniqueResult();
        return ret.intValue();
    }

    private <T> Criteria doFilter( Object filterObject, Class<T> clazz )
    {
        Criteria criteria = sessionFactory.getCurrentSession().createCriteria( Article.class );

        if ( filterObject != null && clazz != null )
        {
            T filter = clazz.cast( filterObject );

            if ( filter instanceof Calendar )
            {
                // The method set(int, int) is undefined for the type T
                filter.set( Calendar.DAY_OF_MONTH, 1 );
                filter.set( Calendar.HOUR_OF_DAY, 0 );
                filter.set( Calendar.MINUTE, 0 );
                filter.set( Calendar.SECOND, 0 );
                // The method getTime() is undefined for the type T
                Date d1 = filter.getTime();

                // The method getActualMaximum(int) is undefined for the type T
                filter.set( Calendar.DAY_OF_MONTH, filter.getActualMaximum( Calendar.DAY_OF_MONTH ) );
                filter.set( Calendar.HOUR_OF_DAY, 23 );
                filter.set( Calendar.MINUTE, 59 );
                filter.set( Calendar.SECOND, 59 );
                Date d2 = filter.getTime();

                criteria.add( Restrictions.between( "creationDate", d1, d2 ) );
            }

            if ( filter instanceof Category )
            {
                // The method getId() is undefined for the type T
                criteria.createCriteria( "categories" ).add( Restrictions.eq( "id", filter.getId() ) );
            }

            if ( filter instanceof Tag )
            {
                // The method getId() is undefined for the type T
                criteria.createCriteria( "tags" ).add( Restrictions.eq( "id", filter.getId() ) );
            }
        }

        return criteria;
    }
}

How ArticleDAO.doFilter should look like? I guess I don’t understand the Generics stuff.

  • 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-09T05:47:15+00:00Added an answer on June 9, 2026 at 5:47 am

    I don’t think generics is buying you anything here. This won’t even compile b/c T is not known and you are trying to call specific methods (e.g. getId()).

    There are a few ways you can do this without generics. The simplest way would be

     if ( filterObject instanceof Calendar ) {
       Calendar filterCal = (Calendar) filterObject;
    
       filterCal.set (Calendar.DAY_OF_MONTH, 1); // ... and so on
    

    If you do it this way, you can drop the clazz parameter from your methods. You don’t need that clazz.cast() call.

    Just a word on generics, I typically find myself using generics when I am doing the same thing regardless of the type. For instance, it looks like you are using generics successfully for your BaseDAO. Regardless of the type, BaseDAO saves, updates, deletes, etc. In your case above, you are trying to do something different based on type. It doesn’t really lend itself to generics. In fact, doing something different per type usually means you can do it nicely with polymorphism.

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

Sidebar

Related Questions

I would like to trim down the routes on my application so that: http://myapplication.com/users/peter/questions/how-do-i-create-urls
I know that Oracle has the TRIM function and I would like to use
Given the following string http://thedude.com/05/simons-cat-and-frog-100x100.jpg I would like to use substr or trim (or
I would like to trim a beginning and ending double quote () from a
Would like to be able to set colors of headings and such, different font
Would like to know what a programmer should know to become a good at
Would like to know the c# code to actually retrieve the IP type: Static
I would like to trim all special characters from a string in SQL. I've
I would like to pass the reference of a variable into a class, use
I would like to trim a \ character from the end of the string.

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.