-
Notifications
You must be signed in to change notification settings - Fork 289
Add ListTenantsPage class. #358
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
248 changes: 248 additions & 0 deletions
248
src/main/java/com/google/firebase/auth/ListTenantsPage.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,248 @@ | ||
/* | ||
* Copyright 2020 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.google.firebase.auth; | ||
|
||
import static com.google.common.base.Preconditions.checkArgument; | ||
import static com.google.common.base.Preconditions.checkNotNull; | ||
|
||
import com.google.api.client.json.JsonFactory; | ||
import com.google.api.gax.paging.Page; | ||
import com.google.common.collect.ImmutableList; | ||
import com.google.firebase.auth.internal.DownloadAccountResponse; | ||
import com.google.firebase.auth.internal.ListTenantsResponse; | ||
import com.google.firebase.internal.NonNull; | ||
import com.google.firebase.internal.Nullable; | ||
import java.util.Iterator; | ||
import java.util.List; | ||
import java.util.NoSuchElementException; | ||
|
||
/** | ||
* Represents a page of {@link Tenant} instances. | ||
* | ||
* <p>Provides methods for iterating over the tenants in the current page, and calling up | ||
* subsequent pages of tenants. | ||
* | ||
* <p>Instances of this class are thread-safe and immutable. | ||
*/ | ||
public class ListTenantsPage implements Page<Tenant> { | ||
|
||
static final String END_OF_LIST = ""; | ||
|
||
private final ListTenantsResponse currentBatch; | ||
private final TenantSource source; | ||
private final int maxResults; | ||
|
||
private ListTenantsPage( | ||
@NonNull ListTenantsResponse currentBatch, @NonNull TenantSource source, int maxResults) { | ||
this.currentBatch = checkNotNull(currentBatch); | ||
this.source = checkNotNull(source); | ||
this.maxResults = maxResults; | ||
} | ||
|
||
/** | ||
* Checks if there is another page of tenants available to retrieve. | ||
* | ||
* @return true if another page is available, or false otherwise. | ||
*/ | ||
@Override | ||
public boolean hasNextPage() { | ||
return !END_OF_LIST.equals(currentBatch.getPageToken()); | ||
} | ||
|
||
/** | ||
* Returns the string token that identifies the next page. | ||
* | ||
* <p>Never returns null. Returns empty string if there are no more pages available to be | ||
* retrieved. | ||
* | ||
* @return A non-null string token (possibly empty, representing no more pages) | ||
*/ | ||
@NonNull | ||
@Override | ||
public String getNextPageToken() { | ||
return currentBatch.getPageToken(); | ||
} | ||
|
||
/** | ||
* Returns the next page of tenants. | ||
* | ||
* @return A new {@link ListTenantsPage} instance, or null if there are no more pages. | ||
*/ | ||
@Nullable | ||
@Override | ||
public ListTenantsPage getNextPage() { | ||
if (hasNextPage()) { | ||
PageFactory factory = new PageFactory(source, maxResults, currentBatch.getPageToken()); | ||
try { | ||
return factory.create(); | ||
} catch (FirebaseAuthException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
/** | ||
* Returns an {@link Iterable} that facilitates transparently iterating over all the tenants in | ||
* the current Firebase project, starting from this page. | ||
* | ||
* <p>The {@link Iterator} instances produced by the returned {@link Iterable} never buffers more | ||
* than one page of tenants at a time. It is safe to abandon the iterators (i.e. break the loops) | ||
* at any time. | ||
* | ||
* @return a new {@link Iterable} instance. | ||
*/ | ||
@NonNull | ||
@Override | ||
public Iterable<Tenant> iterateAll() { | ||
return new TenantIterable(this); | ||
} | ||
|
||
/** | ||
* Returns an {@code Iterable} over the users in this page. | ||
* | ||
* @return a {@code Iterable<Tenant>} instance. | ||
*/ | ||
@NonNull | ||
@Override | ||
public Iterable<Tenant> getValues() { | ||
return currentBatch.getTenants(); | ||
} | ||
|
||
private static class TenantIterable implements Iterable<Tenant> { | ||
|
||
private final ListTenantsPage startingPage; | ||
|
||
TenantIterable(@NonNull ListTenantsPage startingPage) { | ||
this.startingPage = checkNotNull(startingPage, "starting page must not be null"); | ||
} | ||
|
||
@Override | ||
@NonNull | ||
public Iterator<Tenant> iterator() { | ||
return new TenantIterator(startingPage); | ||
} | ||
|
||
/** | ||
* An {@link Iterator} that cycles through tenants, one at a time. | ||
* | ||
* <p>It buffers the last retrieved batch of tenants in memory. The {@code maxResults} parameter | ||
* is an upper bound on the batch size. | ||
*/ | ||
private static class TenantIterator implements Iterator<Tenant> { | ||
|
||
private ListTenantsPage currentPage; | ||
private List<Tenant> batch; | ||
private int index = 0; | ||
|
||
private TenantIterator(ListTenantsPage startingPage) { | ||
setCurrentPage(startingPage); | ||
} | ||
|
||
@Override | ||
public boolean hasNext() { | ||
if (index == batch.size()) { | ||
if (currentPage.hasNextPage()) { | ||
setCurrentPage(currentPage.getNextPage()); | ||
} else { | ||
return false; | ||
} | ||
} | ||
|
||
return index < batch.size(); | ||
} | ||
|
||
@Override | ||
public Tenant next() { | ||
if (!hasNext()) { | ||
throw new NoSuchElementException(); | ||
} | ||
return batch.get(index++); | ||
} | ||
|
||
@Override | ||
public void remove() { | ||
throw new UnsupportedOperationException("remove operation not supported"); | ||
} | ||
|
||
private void setCurrentPage(ListTenantsPage page) { | ||
this.currentPage = checkNotNull(page); | ||
this.batch = ImmutableList.copyOf(page.getValues()); | ||
this.index = 0; | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Represents a source of tenant data that can be queried to load a batch of tenants. | ||
*/ | ||
interface TenantSource { | ||
@NonNull | ||
ListTenantsResponse fetch(int maxResults, String pageToken) | ||
throws FirebaseAuthException; | ||
} | ||
|
||
static class DefaultTenantSource implements TenantSource { | ||
|
||
private final FirebaseUserManager userManager; | ||
private final JsonFactory jsonFactory; | ||
|
||
DefaultTenantSource(FirebaseUserManager userManager, JsonFactory jsonFactory) { | ||
this.userManager = checkNotNull(userManager, "user manager must not be null"); | ||
this.jsonFactory = checkNotNull(jsonFactory, "json factory must not be null"); | ||
} | ||
|
||
@Override | ||
public ListTenantsResponse fetch(int maxResults, String pageToken) | ||
throws FirebaseAuthException { | ||
return userManager.listTenants(maxResults, pageToken); | ||
} | ||
} | ||
|
||
/** | ||
* A simple factory class for {@link ListTenantsPage} instances. | ||
* | ||
* <p>Performs argument validation before attempting to load any tenant data (which is expensive, | ||
* and hence may be performed asynchronously on a separate thread). | ||
*/ | ||
static class PageFactory { | ||
|
||
private final TenantSource source; | ||
private final int maxResults; | ||
private final String pageToken; | ||
|
||
PageFactory(@NonNull TenantSource source) { | ||
this(source, FirebaseUserManager.MAX_LIST_TENANTS_RESULTS, null); | ||
} | ||
|
||
PageFactory(@NonNull TenantSource source, int maxResults, @Nullable String pageToken) { | ||
checkArgument(maxResults > 0 && maxResults <= FirebaseUserManager.MAX_LIST_TENANTS_RESULTS, | ||
"maxResults must be a positive integer that does not exceed %s", | ||
FirebaseUserManager.MAX_LIST_TENANTS_RESULTS); | ||
checkArgument(!END_OF_LIST.equals(pageToken), "invalid end of list page token"); | ||
this.source = checkNotNull(source, "source must not be null"); | ||
this.maxResults = maxResults; | ||
this.pageToken = pageToken; | ||
} | ||
|
||
ListTenantsPage create() throws FirebaseAuthException { | ||
ListTenantsResponse batch = source.fetch(maxResults, pageToken); | ||
return new ListTenantsPage(batch, source, maxResults); | ||
} | ||
} | ||
} | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.