1 package com.fasterxml.jackson.databind.util;
2
3 /**
4  * Helper class used for checking whether a property is visible
5  * in the active view
6  */

7 public class ViewMatcher implements java.io.Serializable
8 {
9     private static final long serialVersionUID = 1L;
10
11     protected final static ViewMatcher EMPTY = new ViewMatcher();
12     
13     public boolean isVisibleForView(Class<?> activeView) { return false; }
14
15     public static ViewMatcher construct(Class<?>[] views)
16     {
17         if (views == null) {
18             return EMPTY;
19         }
20         switch (views.length) {
21         case 0:
22             return EMPTY;
23         case 1:
24             return new Single(views[0]);
25         }
26         return new Multi(views);
27     } 
28     
29     /*
30     /**********************************************************
31     /* Concrete sub-classes
32     /**********************************************************
33      */

34
35     private final static class Single extends ViewMatcher
36     {
37         private static final long serialVersionUID = 1L;
38
39         private final Class<?> _view;
40         public Single(Class<?> v) { _view = v; }
41         @Override
42         public boolean isVisibleForView(Class<?> activeView) {
43             return (activeView == _view) || _view.isAssignableFrom(activeView);
44         }
45     }
46
47     private final static class Multi extends ViewMatcher
48         implements java.io.Serializable
49     {
50         private static final long serialVersionUID = 1L;
51
52         private final Class<?>[] _views;
53
54         public Multi(Class<?>[] v) { _views = v; }
55
56         @Override
57         public boolean isVisibleForView(Class<?> activeView)
58         {
59             for (int i = 0, len = _views.length; i < len; ++i) {
60                 Class<?> view = _views[i];
61                 if ((activeView == view) || view.isAssignableFrom(activeView)) {
62                     return true;
63                 }
64             }
65             return false;
66         }
67     }
68 }
69