-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #368 from ajkannan/pull-master-updates
Update resource-manager branch
- Loading branch information
Showing
63 changed files
with
2,253 additions
and
804 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
54 changes: 54 additions & 0 deletions
54
gcloud-java-core/src/main/java/com/google/gcloud/BaseServiceException.java
This file contains 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,54 @@ | ||
/* | ||
* Copyright 2015 Google Inc. All Rights Reserved. | ||
* | ||
* 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.gcloud; | ||
|
||
/** | ||
* Base class for all service exceptions. | ||
*/ | ||
public class BaseServiceException extends RuntimeException { | ||
|
||
private static final long serialVersionUID = 5028833760039966178L; | ||
|
||
private final int code; | ||
private final boolean retryable; | ||
|
||
public BaseServiceException(int code, String message, boolean retryable) { | ||
super(message); | ||
this.code = code; | ||
this.retryable = retryable; | ||
} | ||
|
||
public BaseServiceException(int code, String message, boolean retryable, Exception cause) { | ||
super(message, cause); | ||
this.code = code; | ||
this.retryable = retryable; | ||
} | ||
|
||
/** | ||
* Returns the code associated with this exception. | ||
*/ | ||
public int code() { | ||
return code; | ||
} | ||
|
||
/** | ||
* Returns {@code true} when it is safe to retry the operation that caused this exception. | ||
*/ | ||
public boolean retryable() { | ||
return retryable; | ||
} | ||
} |
This file contains 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
67 changes: 67 additions & 0 deletions
67
gcloud-java-core/src/main/java/com/google/gcloud/Page.java
This file contains 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,67 @@ | ||
/* | ||
* Copyright 2015 Google Inc. All Rights Reserved. | ||
* | ||
* 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.gcloud; | ||
|
||
import java.util.Iterator; | ||
|
||
/** | ||
* Interface for Google Cloud paginated results. | ||
* | ||
* <p> | ||
* Use {@code Page} to iterate through all values (also in next pages): | ||
* <pre> {@code | ||
* Page<T> page = ...; // get a Page<T> instance | ||
* Iterator<T> iterator = page.iterateAll(); | ||
* while (iterator.hasNext()) { | ||
* T value = iterator.next(); | ||
* // do something with value | ||
* }}</pre> | ||
* <p> | ||
* Or handle pagination explicitly: | ||
* <pre> {@code | ||
* Page<T> page = ...; // get a Page<T> instance | ||
* while (page != null) { | ||
* for (T value : page.values()) { | ||
* // do something with value | ||
* } | ||
* page = page.nextPage(); | ||
* }}</pre> | ||
*/ | ||
public interface Page<T> { | ||
|
||
/** | ||
* Returns the values contained in this page. | ||
*/ | ||
Iterable<T> values(); | ||
|
||
/** | ||
* Returns an iterator for all values, possibly also in the next pages. Once current page's values | ||
* are traversed the iterator fetches next page, if any. | ||
*/ | ||
Iterator<T> iterateAll(); | ||
|
||
/** | ||
* Returns the cursor for the nextPage or {@code null} if no more results. | ||
*/ | ||
String nextPageCursor(); | ||
|
||
/** | ||
* Returns the next page of results or {@code null} if no more result. | ||
*/ | ||
Page<T> nextPage(); | ||
|
||
} |
111 changes: 111 additions & 0 deletions
111
gcloud-java-core/src/main/java/com/google/gcloud/PageImpl.java
This file contains 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,111 @@ | ||
/* | ||
* Copyright 2015 Google Inc. All Rights Reserved. | ||
* | ||
* 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.gcloud; | ||
|
||
import com.google.common.collect.AbstractIterator; | ||
|
||
import java.io.Serializable; | ||
import java.util.Collections; | ||
import java.util.Iterator; | ||
import java.util.Objects; | ||
|
||
/** | ||
* Base implementation for Google Cloud paginated results. | ||
*/ | ||
public class PageImpl<T> implements Page<T>, Serializable { | ||
|
||
private static final long serialVersionUID = 3914827379823557934L; | ||
|
||
private final String cursor; | ||
private final Iterable<T> results; | ||
private final NextPageFetcher<T> pageFetcher; | ||
|
||
public interface NextPageFetcher<T> extends Serializable { | ||
Page<T> nextPage(); | ||
} | ||
|
||
static class PageIterator<T> extends AbstractIterator<T> { | ||
|
||
private Iterator<T> currentPageIterator; | ||
private Page<T> currentPage; | ||
|
||
PageIterator(Page<T> currentPage) { | ||
this.currentPageIterator = currentPage.values().iterator(); | ||
this.currentPage = currentPage; | ||
} | ||
|
||
@Override | ||
protected T computeNext() { | ||
while (!currentPageIterator.hasNext()) { | ||
currentPage = currentPage.nextPage(); | ||
if (currentPage == null) { | ||
return endOfData(); | ||
} | ||
currentPageIterator = currentPage.values().iterator(); | ||
} | ||
return currentPageIterator.next(); | ||
} | ||
} | ||
|
||
/** | ||
* Creates a {@code PageImpl} object. In order for the object to be serializable the {@code | ||
* results} parameter must be serializable. | ||
*/ | ||
public PageImpl(NextPageFetcher<T> pageFetcher, String cursor, Iterable<T> results) { | ||
this.pageFetcher = pageFetcher; | ||
this.cursor = cursor; | ||
this.results = results; | ||
} | ||
|
||
@Override | ||
public Iterable<T> values() { | ||
return results == null ? Collections.EMPTY_LIST : results; | ||
} | ||
|
||
@Override | ||
public Iterator<T> iterateAll() { | ||
return new PageIterator<T>(this); | ||
} | ||
|
||
@Override | ||
public String nextPageCursor() { | ||
return cursor; | ||
} | ||
|
||
@Override | ||
public Page<T> nextPage() { | ||
if (cursor == null || pageFetcher == null) { | ||
return null; | ||
} | ||
return pageFetcher.nextPage(); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(cursor, results); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object obj) { | ||
if (!(obj instanceof PageImpl)) { | ||
return false; | ||
} | ||
PageImpl<?> other = (PageImpl<?>) obj; | ||
return Objects.equals(cursor, other.cursor) | ||
&& Objects.equals(results, other.results); | ||
} | ||
} |
Oops, something went wrong.