Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 2012-2013 the original author or authors.
   *
   * 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 org.springframework.hateoas;
 
 import java.util.Map;
 
 
Value object for links.

Author(s):
Oliver Gierke
 
 @XmlType(name = "link", namespace = .)
 public class Link implements Serializable {
 
 	private static final long serialVersionUID = -9037755944661782121L;
 
 	public static final String ATOM_NAMESPACE = "http://www.w3.org/2005/Atom";
 
 	public static final String REL_SELF = "self";
 	public static final String REL_FIRST = "first";
 	public static final String REL_PREVIOUS = "prev";
 	public static final String REL_NEXT = "next";
 	public static final String REL_LAST = "last";
 
 	private String rel;
 	private String href;

Creates a new link to the given URI with the self rel.

Parameters:
href must not be null or empty.
See also:
REL_SELF
 
 	public Link(String href) {
 		this(href);
 	}

Creates a new Link to the given URI with the given rel.

Parameters:
href must not be null or empty.
rel must not be null or empty.
 
 	public Link(String hrefString rel) {
 
 		Assert.hasText(href"Href must not be null or empty!");
 		Assert.hasText(rel"Rel must not be null or empty!");
 
 		this. = href;
 		this. = rel;
 	}

Empty constructor required by the marshalling framework.
 
 	protected Link() {
 
 	}

Returns the actual URI the link is pointing to.

Returns:
 
 	public String getHref() {
 		return ;
 	}

Returns the rel of the link.

Returns:
 
	public String getRel() {
		return ;
	}

Returns a Link pointing to the same URI but with the given relation.

Parameters:
rel must not be null or empty.
Returns:
	public Link withRel(String rel) {
		return new Link(rel);
	}

Returns a Link pointing to the same URI but with the self relation.

Returns:
	public Link withSelfRel() {
	}
	/* 
	 * (non-Javadoc)
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	public boolean equals(Object obj) {
		if (this == obj) {
			return true;
		}
		if (!(obj instanceof Link)) {
			return false;
		}
		Link that = (Linkobj;
		return this..equals(that.href) && this..equals(that.rel);
	}
	/* 
	 * (non-Javadoc)
	 * @see java.lang.Object#hashCode()
	 */
	public int hashCode() {
		int result = 17;
		result += 31 * .hashCode();
		result += 31 * .hashCode();
		return result;
	}
	/* 
	 * (non-Javadoc)
	 * @see java.lang.Object#toString()
	 */
	public String toString() {
		return String.format("<%s>;rel=\"%s\"");
	}

Factory method to easily create Link instances from RFC-5988 compatible java.lang.String representations of a link. Will return null if an empty or null java.lang.String is given.

Parameters:
element an RFC-5899 compatible representation of a link.
Returns:
Throws:
java.lang.IllegalArgumentException if a non-empty java.lang.String was given that does not adhere to RFC-5899.
java.lang.IllegalArgumentException if no rel attribute could be found.
	public static Link valueOf(String element) {
		if (!StringUtils.hasText(element)) {
			return null;
		}
		Pattern uriAndAttributes = Pattern.compile("<(.*)>;(.*)");
		Matcher matcher = uriAndAttributes.matcher(element);
		if (matcher.find()) {
			Map<StringStringattributes = getAttributeMap(matcher.group(2));
			if (!attributes.containsKey("rel")) {
				throw new IllegalArgumentException("Link does not provide a rel attribute!");
			}
			return new Link(matcher.group(1), attributes.get("rel"));
else {
			throw new IllegalArgumentException(String.format("Given link header %s is not RFC5988 compliant!"element));
		}
	}

Parses the links attributes from the given source java.lang.String.

Parameters:
source
Returns:
	private static Map<StringStringgetAttributeMap(String source) {
		if (!StringUtils.hasText(source)) {
			return Collections.emptyMap();
		}
		Map<StringStringattributes = new HashMap<StringString>();
		Pattern keyAndValue = Pattern.compile("(\\w+)=\\\"(\\p{Alnum}*)\"");
		Matcher matcher = keyAndValue.matcher(source);
		while (matcher.find()) {
			attributes.put(matcher.group(1), matcher.group(2));
		}
		return attributes;
	}
New to GrepCode? Check out our FAQ X