org.dmfs.rfc3986.uris.Text Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of rfc3986-uri Show documentation
Show all versions of rfc3986-uri Show documentation
RFC 3986 compliant URI implementation.
/*
* Copyright 2017 dmfs GmbH
*
* 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.dmfs.rfc3986.uris;
import org.dmfs.rfc3986.Authority;
import org.dmfs.rfc3986.Fragment;
import org.dmfs.rfc3986.Query;
import org.dmfs.rfc3986.Uri;
import org.dmfs.rfc3986.encoding.Special;
/**
* Adapter to adapt a {@link Uri} to a {@link CharSequence}.
*
* @author Marten Gajda
*/
public final class Text implements CharSequence
{
private final Uri mUri;
private String mString;
public Text(Uri uri)
{
mUri = uri;
}
@Override
public int length()
{
return toString().length();
}
@Override
public char charAt(int i)
{
return toString().charAt(i);
}
@Override
public CharSequence subSequence(int i, int i1)
{
return toString().subSequence(i, i1);
}
@Override
public String toString()
{
if (mString == null)
{
StringBuilder stringBuilder = new StringBuilder(128);
if (mUri.scheme().isPresent())
{
stringBuilder.append(mUri.scheme().value(Special.EMPTY).toString());
stringBuilder.append(':');
}
if (mUri.authority() != null)
{
stringBuilder.append("//");
Authority authority = mUri.authority();
if (authority.userinfo() != null)
{
stringBuilder.append(authority.userinfo());
stringBuilder.append('@');
}
stringBuilder.append(authority.host());
if (authority.port() >= 0)
{
stringBuilder.append(':');
stringBuilder.append(Integer.valueOf(authority.port()));
}
}
stringBuilder.append(new org.dmfs.rfc3986.paths.Text(mUri.path()));
Query query = mUri.query();
if (query.isPresent())
{
stringBuilder.append('?');
stringBuilder.append(query.value(Special.EMPTY));
}
Fragment fragment = mUri.fragment();
if (fragment.isPresent())
{
stringBuilder.append('#');
stringBuilder.append(fragment.value(Special.EMPTY));
}
mString = stringBuilder.toString();
}
return mString;
}
}