<commit_before>package com.cognifide.slice.commons.link;

/*
 * #%L
 * Slice - Core
 * $Id:$
 * $HeadURL:$
 * %%
 * Copyright (C) 2012 Cognifide Limited
 * %%
 * 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.
 * #L%
 */
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.regex.Pattern;

import org.apache.commons.lang.StringUtils;

import com.cognifide.slice.api.link.Link;
import com.cognifide.slice.api.link.LinkBuilder;
import com.cognifide.slice.core.internal.link.LinkImpl;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * Allows building links and modifying existing link. Use whenever you need to add/remove selectors, query
 * strings, suffix etc.
 *
 * @author <NAME>
 * @author maciej.majchrzak
 */
public final class LinkBuilderImpl implements LinkBuilder {

	private String path;

	private List<String> selectors;

	private String extension;

	private String suffix;

	private String fragment;

	private Map<String, List<String>> queries;

	private String protocol;

	private String domain;

	/**
	 * Creates an empty builder. All parts of a link are empty.
	 */
	public LinkBuilderImpl() {
		this.path = "";
		this.selectors = new ArrayList<String>();
		this.queries = new HashMap<String, List<String>>();
		this.extension = "";
		this.suffix = "";
		this.fragment = "";
	}

	/**
	 * Creates a builder and sets underlying values using specified link. The values can be modified without
	 * any affection on specified link.
	 *
	 * @param link
	 */
	public LinkBuilderImpl(final Link link) {
		this.path = link.getPath();
		List<String> linkSelectors = link.getSelectors();
		this.selectors = new ArrayList<String>(linkSelectors.size());
		this.selectors.addAll(linkSelectors);
		this.extension = link.getExtension();
		this.suffix = link.getSuffix();
		setQueryString(link.getQueryString());
		this.fragment = link.getFragment();
	}

	/**
	 * Creates a builder and sets underlying values using specified URL.
	 *
	 * @param url
	 * @throws java.net.MalformedURLException
	 */
	public LinkBuilderImpl(final String url) throws MalformedURLException {
		URL urlHelper = new URL(url);
		String urlPath = urlHelper.getPath();
		if (StringUtils.countMatches(urlPath, ".") > 1
				|| StringUtils.lastIndexOf(urlPath, "/") > StringUtils.lastIndexOf(urlPath, ".")) {
			String[] split = urlPath.split("\\.[a-zA-Z]+\\/");
			if (split.length == 2) {
				urlPath = StringUtils.substringBeforeLast(urlPath, "/" + split[1]);
				this.suffix = "/" + split[1];
			} else {
				suffix = "";
			}
		}
		this.path = StringUtils.substringBefore(urlPath, ".");
		this.extension = StringUtils.substringAfterLast(urlPath, ".");
		setSelectorString(StringUtils.substringBetween(urlPath, path, extension));
		setQueryString(urlHelper.getQuery());
		this.fragment = (urlHelper.getRef() == null) ? "" : urlHelper.getRef();
	}

	/**
	 * Returns {@link LinkImpl} representing data in the builder.
	 *
	 * @return {@link LinkImpl} representing data in the builder, never null
	 */
	@Override
	public Link toLink() {
		return new LinkImpl(protocol, domain, path, Collections.unmodifiableList(selectors), extension,
				suffix, getQueryString(), fragment);<commit_msg>SLICE-56 removing unused imports<commit_after>package com.cognifide.slice.commons.link;

/*
 * #%L
 * Slice - Core
 * $Id:$
 * $HeadURL:$
 * %%
 * Copyright (C) 2012 Cognifide Limited
 * %%
 * 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.
 * #L%
 */
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.regex.Pattern;

import org.apache.commons.lang.StringUtils;

import com.cognifide.slice.api.link.Link;
import com.cognifide.slice.api.link.LinkBuilder;
import com.cognifide.slice.core.internal.link.LinkImpl;
import java.net.MalformedURLException;
import java.net.URL;

/**
 * Allows building links and modifying existing link. Use whenever you need to add/remove selectors, query
 * strings, suffix etc.
 *
 * @author <NAME>
 * @author maciej.majchrzak
 */
public final class LinkBuilderImpl implements LinkBuilder {

	private String path;

	private List<String> selectors;

	private String extension;

	private String suffix;

	private String fragment;

	private Map<String, List<String>> queries;

	private String protocol;

	private String domain;

	/**
	 * Creates an empty builder. All parts of a link are empty.
	 */
	public LinkBuilderImpl() {
		this.path = "";
		this.selectors = new ArrayList<String>();
		this.queries = new HashMap<String, List<String>>();
		this.extension = "";
		this.suffix = "";
		this.fragment = "";
	}

	/**
	 * Creates a builder and sets underlying values using specified link. The values can be modified without
	 * any affection on specified link.
	 *
	 * @param link
	 */
	public LinkBuilderImpl(final Link link) {
		this.path = link.getPath();
		List<String> linkSelectors = link.getSelectors();
		this.selectors = new ArrayList<String>(linkSelectors.size());
		this.selectors.addAll(linkSelectors);
		this.extension = link.getExtension();
		this.suffix = link.getSuffix();
		setQueryString(link.getQueryString());
		this.fragment = link.getFragment();
	}

	/**
	 * Creates a builder and sets underlying values using specified URL.
	 *
	 * @param url
	 */
	public LinkBuilderImpl(final String url) throws MalformedURLException {
		URL urlHelper = new URL(url);
		String urlPath = urlHelper.getPath();
		if (StringUtils.countMatches(urlPath, ".") > 1
				|| StringUtils.lastIndexOf(urlPath, "/") > StringUtils.lastIndexOf(urlPath, ".")) {
			String[] split = urlPath.split("\\.[a-zA-Z]+\\/");
			if (split.length == 2) {
				urlPath = StringUtils.substringBeforeLast(urlPath, "/" + split[1]);
				this.suffix = "/" + split[1];
			} else {
				suffix = "";
			}
		}
		this.path = StringUtils.substringBefore(urlPath, ".");
		this.extension = StringUtils.substringAfterLast(urlPath, ".");
		setSelectorString(StringUtils.substringBetween(urlPath, path, extension));
		setQueryString(urlHelper.getQuery());
		this.fragment = (urlHelper.getRef() == null) ? "" : urlHelper.getRef();
	}

	/**
	 * Returns {@link LinkImpl} representing data in the builder.
	 *
	 * @return {@link LinkImpl} representing data in the builder, never null
	 */
	@Override
	public Link toLink() {
		return new LinkImpl(protocol, domain, path, Collections.unmodifiableList(selectors), extension,
				suffix, getQueryString(), fragment);
{'fraction_non_alphanumeric': 0.12495124171109089, 'fraction_numerical': 0.003380574697698609, 'mean_word_length': 4.087301587301587, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2500328', 'n_tokens_mistral': 2445, 'n_tokens_neox': 2254, 'n_words': 660}