Permalink
Browse files

Merge pull request #39 from christav/type-safe-links

Make LinkInfo generic for type safety
  • Loading branch information...
2 parents 9a0d6fb + c66d567 commit 079d78e549a663942d5750532e4d8f00e5dd49dd @christav christav committed Jan 9, 2013
View
26 ...i/src/main/java/com/microsoft/windowsazure/services/media/implementation/ODataEntity.java
@@ -17,8 +17,6 @@
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
-import java.util.ArrayList;
-import java.util.List;
import javax.xml.bind.JAXBElement;
@@ -88,12 +86,12 @@ public boolean hasLink(String rel) {
* rel of link to retrieve
* @return The link if found, null if not.
*/
- public LinkInfo getLink(String rel) {
+ public <U extends ODataEntity<?>> LinkInfo<U> getLink(String rel) {
for (Object child : entry.getEntryChildren()) {
LinkType link = LinkFromChild(child);
if (link != null && link.getRel().equals(rel)) {
- return new LinkInfo(link);
+ return new LinkInfo<U>(link);
}
}
return null;
@@ -106,24 +104,8 @@ public LinkInfo getLink(String rel) {
* name of the OData relationship
* @return the link if found, null if not.
*/
- public LinkInfo getRelationLink(String relationName) {
- return getLink(Constants.ODATA_DATA_NS + "/related/" + relationName);
- }
-
- /**
- * Return the links from this entry
- *
- * @return List of the links.
- */
- public List<LinkInfo> getLinks() {
- ArrayList<LinkInfo> links = new ArrayList<LinkInfo>();
- for (Object child : entry.getEntryChildren()) {
- LinkType link = LinkFromChild(child);
- if (link != null) {
- links.add(new LinkInfo(link));
- }
- }
- return links;
+ public <U extends ODataEntity<?>> LinkInfo<U> getRelationLink(String relationName) {
+ return this.<U> getLink(Constants.ODATA_DATA_NS + "/related/" + relationName);
}
@SuppressWarnings("rawtypes")
View
2 ...zure-api/src/main/java/com/microsoft/windowsazure/services/media/models/AccessPolicy.java
@@ -97,7 +97,7 @@ public Object getRequestContents() {
* the link
* @return the operation
*/
- public static EntityGetOperation<AccessPolicyInfo> get(LinkInfo link) {
+ public static EntityGetOperation<AccessPolicyInfo> get(LinkInfo<AccessPolicyInfo> link) {
return new DefaultGetOperation<AccessPolicyInfo>(link.getHref(), AccessPolicyInfo.class);
}
View
4 ...osoft-azure-api/src/main/java/com/microsoft/windowsazure/services/media/models/Asset.java
@@ -171,7 +171,7 @@ public Creator setState(AssetState state) {
* the link
* @return the get operation
*/
- public static EntityGetOperation<AssetInfo> get(LinkInfo link) {
+ public static EntityGetOperation<AssetInfo> get(LinkInfo<AssetInfo> link) {
return new DefaultGetOperation<AssetInfo>(link.getHref(), AssetInfo.class);
}
@@ -204,7 +204,7 @@ public Creator setState(AssetState state) {
* Link to request assets from.
* @return The list operation.
*/
- public static DefaultListOperation<AssetInfo> list(LinkInfo link) {
+ public static DefaultListOperation<AssetInfo> list(LinkInfo<AssetInfo> link) {
return new DefaultListOperation<AssetInfo>(link.getHref(), new GenericType<ListResult<AssetInfo>>() {
});
}
View
2 ...t-azure-api/src/main/java/com/microsoft/windowsazure/services/media/models/AssetFile.java
@@ -207,7 +207,7 @@ public static EntityActionOperation createFileInfos(String assetId) {
* Link to request AssetFiles from.
* @return The list operation.
*/
- public static DefaultListOperation<AssetFileInfo> list(LinkInfo link) {
+ public static DefaultListOperation<AssetFileInfo> list(LinkInfo<AssetFileInfo> link) {
return new DefaultListOperation<AssetFileInfo>(link.getHref(), new GenericType<ListResult<AssetFileInfo>>() {
});
}
View
18 ...t-azure-api/src/main/java/com/microsoft/windowsazure/services/media/models/AssetInfo.java
@@ -107,34 +107,34 @@ public AssetOption getOptions() {
*
* @return the link
*/
- public LinkInfo getAssetFilesLink() {
- return getRelationLink("Files");
+ public LinkInfo<AssetFileInfo> getAssetFilesLink() {
+ return this.<AssetFileInfo> getRelationLink("Files");
}
/**
* Get a link to the asset's content keys
*
* @return the link
*/
- public LinkInfo getContentKeysLink() {
- return getRelationLink("ContentKeys");
+ public LinkInfo<ContentKeyInfo> getContentKeysLink() {
+ return this.<ContentKeyInfo> getRelationLink("ContentKeys");
}
/**
* Get a link to the asset's locators
*
* @return the link
*/
- public LinkInfo getLocatorsLink() {
- return getRelationLink("Locators");
+ public LinkInfo<LocatorInfo> getLocatorsLink() {
+ return this.<LocatorInfo> getRelationLink("Locators");
}
/**
- * Get a link to the asset's locators
+ * Get a link to this asset's parents
*
* @return the link
*/
- public LinkInfo getParentAssetsLink() {
- return getRelationLink("ParentAssets");
+ public LinkInfo<AssetInfo> getParentAssetsLink() {
+ return this.<AssetInfo> getRelationLink("ParentAssets");
}
}
View
2 ...-azure-api/src/main/java/com/microsoft/windowsazure/services/media/models/ContentKey.java
@@ -211,7 +211,7 @@ public Creator setProtectionKeyType(ProtectionKeyType protectionKeyType) {
* Link to request content keys from.
* @return The list operation.
*/
- public static DefaultListOperation<ContentKeyInfo> list(LinkInfo link) {
+ public static DefaultListOperation<ContentKeyInfo> list(LinkInfo<ContentKeyInfo> link) {
return new DefaultListOperation<ContentKeyInfo>(link.getHref(), new GenericType<ListResult<ContentKeyInfo>>() {
});
}
View
12 ...oft-azure-api/src/main/java/com/microsoft/windowsazure/services/media/models/JobInfo.java
@@ -133,25 +133,25 @@ public String getTemplateId() {
*
* @return Link if found, null if not.
*/
- public LinkInfo getInputAssetsLink() {
- return getRelationLink("InputMediaAssets");
+ public LinkInfo<AssetInfo> getInputAssetsLink() {
+ return this.<AssetInfo> getRelationLink("InputMediaAssets");
}
/**
* Get a link which, when listed, will return the output assets for the job.
*
* @return Link if found, null if not.
*/
- public LinkInfo getOutputAssetsLink() {
- return getRelationLink("OutputMediaAssets");
+ public LinkInfo<AssetInfo> getOutputAssetsLink() {
+ return this.<AssetInfo> getRelationLink("OutputMediaAssets");
}
/**
* Gets the tasks link.
*
* @return the tasks link
*/
- public LinkInfo getTasksLink() {
- return getRelationLink("Tasks");
+ public LinkInfo<TaskInfo> getTasksLink() {
+ return this.<TaskInfo> getRelationLink("Tasks");
}
}
View
3 ...ft-azure-api/src/main/java/com/microsoft/windowsazure/services/media/models/LinkInfo.java
@@ -15,13 +15,14 @@
package com.microsoft.windowsazure.services.media.models;
+import com.microsoft.windowsazure.services.media.implementation.ODataEntity;
import com.microsoft.windowsazure.services.media.implementation.atom.LinkType;
/**
* Provides access to OData links
*
*/
-public class LinkInfo {
+public class LinkInfo<T extends ODataEntity<?>> {
private final LinkType rawLink;
/**
View
2 ...oft-azure-api/src/main/java/com/microsoft/windowsazure/services/media/models/Locator.java
@@ -218,7 +218,7 @@ public Creator setContentAccessComponent(String contentAccessComponent) {
* Link to request locators from.
* @return The list operation.
*/
- public static DefaultListOperation<LocatorInfo> list(LinkInfo link) {
+ public static DefaultListOperation<LocatorInfo> list(LinkInfo<LocatorInfo> link) {
return new DefaultListOperation<LocatorInfo>(link.getHref(), new GenericType<ListResult<LocatorInfo>>() {
});
}
View
8 ...azure-api/src/main/java/com/microsoft/windowsazure/services/media/models/LocatorInfo.java
@@ -124,16 +124,16 @@ public String getContentAccessToken() {
*
* @return the link
*/
- public LinkInfo getAccessPolicyLink() {
- return getRelationLink("AccessPolicy");
+ public LinkInfo<AccessPolicyInfo> getAccessPolicyLink() {
+ return this.<AccessPolicyInfo> getRelationLink("AccessPolicy");
}
/**
* Return a link that gets this locator's asset
*
* @return the link
*/
- public LinkInfo getAssetLink() {
- return getRelationLink("Asset");
+ public LinkInfo<AssetInfo> getAssetLink() {
+ return this.<AssetInfo> getRelationLink("Asset");
}
}
View
2 microsoft-azure-api/src/main/java/com/microsoft/windowsazure/services/media/models/Task.java
@@ -80,7 +80,7 @@ public static CreateBatchOperation create(String mediaProcessorId, String taskBo
* link to tasks
* @return the list operation.
*/
- public static DefaultListOperation<TaskInfo> list(LinkInfo link) {
+ public static DefaultListOperation<TaskInfo> list(LinkInfo<TaskInfo> link) {
return new DefaultListOperation<TaskInfo>(link.getHref(), new GenericType<ListResult<TaskInfo>>() {
});
}
View
8 ...ft-azure-api/src/main/java/com/microsoft/windowsazure/services/media/models/TaskInfo.java
@@ -217,16 +217,16 @@ public String getInitializationVector() {
*
* @return the link
*/
- public LinkInfo getInputAssetsLink() {
- return getRelationLink("InputMediaAssets");
+ public LinkInfo<AssetInfo> getInputAssetsLink() {
+ return this.<AssetInfo> getRelationLink("InputMediaAssets");
}
/**
* Gets link to the task's output assets
*
* @return the link
*/
- public LinkInfo getOutputAssetsLink() {
- return getRelationLink("OutputMediaAssets");
+ public LinkInfo<AssetInfo> getOutputAssetsLink() {
+ return this.<AssetInfo> getRelationLink("OutputMediaAssets");
}
}
View
11 ...test/java/com/microsoft/windowsazure/services/media/implementation/LinkRetrievalTest.java
@@ -17,8 +17,6 @@
import static org.junit.Assert.*;
-import java.util.List;
-
import javax.xml.bind.JAXBElement;
import javax.xml.namespace.QName;
@@ -96,15 +94,6 @@ public void getNullWhenLinkIsntThere() {
assertNull(info.getLink("noSuchLink"));
}
- @Test
- public void getLinksReturnsTwoExpectedLinksInOrder() {
- List<LinkInfo> links = info.getLinks();
-
- assertEquals(2, links.size());
- assertLinksEqual(link1, links.get(0));
- assertLinksEqual(link2, links.get(1));
- }
-
private static void assertLinksEqual(LinkType expected, LinkInfo actual) {
assertEquals(expected.getHref(), actual.getHref());
}

0 comments on commit 079d78e

Please sign in to comment.