1 /*******************************************************************************
2 * Copyright 2012 Fat Cat
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 ******************************************************************************/
16 package com
.assembla
.ariane
.jellyfish
.proxy
.vfc
;
18 import java
.io
.Serializable
;
19 import java
.util
.Collections
;
20 import java
.util
.LinkedList
;
21 import java
.util
.List
;
23 import java
.util
.TreeMap
;
24 import com
.assembla
.ariane
.jellyfish
.util
.collections
.Pair
;
27 * This class contains an list of users.
32 public final class Users
implements Serializable
35 /** The class version. */
36 private static final long serialVersionUID
= -6247057140716941819L;
38 /** The list of users. */
39 private List
<User
> users
;
41 /** The singleton object. */
42 private static final Users inst
= new Users( );
45 * Returns the instance of this singleton.
46 * @return The users singleton.
48 public static Users
instance( )
54 * Creates a new user list.
58 this.users
= Collections
.synchronizedList( new LinkedList
< User
>( ) );
62 * Returns an read-only list of all users.
63 * @return An list of all users.
65 public synchronized List
< User
> getUsers( )
67 return Collections
.unmodifiableList( this.users
);
71 * Checks if the given user name is known.
72 * @param username The name of the user to look for.
73 * @return True, if the user exists.
75 public synchronized boolean hasUserName( final String username
)
77 for( User user
: this.users
)
79 if( user
.getUserName( ).equals( username
) )
88 * Returns the user with the given name, or null if the user does not exist.
89 * @param username The name of the user to return.
90 * @return The corresponding user.
92 public synchronized User
getUserFor( final String username
)
94 for( User user
: this.users
)
96 if( user
.getUserName( ).equals( username
) )
105 * Adds a user to the list of all users.
106 * @param user The user to add to the list.
108 public synchronized void addUser( final User user
)
110 if( ( user
!= null ) &&
111 ( user
.getPivots( ).isEmpty( ) ) &&
112 !this.users
.contains( user
) )
114 this.users
.add( user
);
119 * Removes the given user from the user list.
120 * @param user The user to remove.
122 public synchronized void removeUser( final User user
)
124 if( ( user
!= null ) && this.users
.contains( user
) )
126 this.users
.remove( user
);
131 * Checks if the needed page needs remapping.
132 * @param page The page to check.
133 * @return True, if the page needs remapping.
135 public boolean isRemapNeeded( final VFCPage page
)
137 for( User user
: this.users
)
139 if( user
.isRemapNeeded( page
) )
148 * Remapps the given page.
149 * @param page The page to remap.
151 public void remapAsNeeded( final VFCPage page
)
153 for( User user
: this.users
)
155 user
.remapAsNeeded( page
);
160 * Returns an list of pivots for the given page, grouped by user.
161 * @param page The page to return the pivot list.
162 * @return An pivot list, grouped by user.
164 public Map
<User
, List
<Pivot
>> getPivotsFor( final VFCPage page
)
166 final Map
< User
, List
<Pivot
> > map
= new TreeMap
< User
, List
<Pivot
> >( User
.USER_NAME_COMPARATOR
);
168 for( User user
: this.users
)
170 if( user
.isPageKnown( page
) )
172 map
.put( user
, user
.getPivotsFor( page
) );
176 return Collections
.unmodifiableMap( map
);
180 * Returns if the page url is known (and if it is known for the given user or not).
181 * @param url The page url to look for.
182 * @param user The user that should known about the page.
183 * @return An object with the collected information.
185 public PageState
isPageKnown( final String url
, final User user
)
187 boolean pageKnown
= false;
188 for( User currentUser
: this.users
)
190 if( currentUser
.isPageKnown( url
) )
192 if ( User
.USER_NAME_COMPARATOR
.compare( user
, currentUser
) == 0 )
194 return PageState
.PAGE_KNOWN_FOR_USER
;
204 return PageState
.PAGE_KNOWN
;
207 return PageState
.PAGE_UNKNOWN
;
211 * Returns the page with the given url. (Checking also if the given user known the page )
212 * @param url The url of the page to return.
213 * @param user The user that should known the page.
214 * @return An tuple with information about the user containing the page,
215 * and the page with the given url.
217 public Pair
<PageState
, VFCPage
> peekPageInformation( final String url
, final User user
)
220 VFCPage compPage
= null;
222 for( User currentUser
: this.users
)
224 compPage
= currentUser
.peekPageInformation( url
);
225 if( compPage
!= null )
227 if ( User
.USER_NAME_COMPARATOR
.compare( user
, currentUser
) == 0 )
229 return new Pair
<PageState
, VFCPage
>( PageState
.PAGE_KNOWN_FOR_USER
, compPage
);
239 return new Pair
<PageState
, VFCPage
>( PageState
.PAGE_KNOWN
, page
);
241 return new Pair
<PageState
, VFCPage
>( PageState
.PAGE_UNKNOWN
, null );