<commit_before>/*
 * Copyright 2011-2014 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.social.vkontakte.api.impl;

import java.net.URI;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Properties;

import com.fasterxml.jackson.databind.ObjectMapper;

import org.springframework.web.client.RestTemplate;

import org.springframework.social.vkontakte.api.ApiVersion;
import org.springframework.social.vkontakte.api.City;
import org.springframework.social.vkontakte.api.CityList;
import org.springframework.social.vkontakte.api.LocationOperations;

/**
 * Implementation VK geolocation API.
 * 
 * @see http://vk.com/dev/places
 * @author badbob
 */
public class LocationTemplate extends AbstractVKontakteOperations implements LocationOperations {

	private final RestTemplate restTemplate;

	public LocationTemplate(RestTemplate restTemplate, String accessToken, ObjectMapper objectMapper, boolean isAuthorizedForUser) {
	        super(isAuthorizedForUser, accessToken, objectMapper);
		this.restTemplate = restTemplate;
	}
	
	@Override
	public City getCityById(Integer id) {
		List<City> cities = getCityById(Collections.singletonList(id));
		return cities.size() > 0 ? cities.get(0) : null;
	}

    @Override
    public List<City> getCityById(Collection<Integer> ids) {
        String idsString = buildCidsAsString(ids);
        Properties props = new Properties();

        props.put("cids", idsString);
        URI uri = makeOperationURL("places.getCityById", props, ApiVersion.VERSION_3_0);

        CityList cityList = restTemplate.getForObject(uri, CityList.class);
        assert cityList != null;
        checkForError(cityList);

        return cityList.getCities();
    }


    // This method compile a coma-separated string from collection of integer city identifier.
    // Actually, this method do the same thing as org.apache.commons.lang.StringUtils.join(ids, ',').
    // But, I decide, that there is to many overheads to include commons-lang lib to use one single method.
	private String buildCidsAsString(Collection<Integer> ids) {
		String delim = "";<commit_msg>fix javadoc<commit_after>/*
 * Copyright 2011-2014 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.social.vkontakte.api.impl;

import java.net.URI;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Properties;

import com.fasterxml.jackson.databind.ObjectMapper;

import org.springframework.web.client.RestTemplate;

import org.springframework.social.vkontakte.api.ApiVersion;
import org.springframework.social.vkontakte.api.City;
import org.springframework.social.vkontakte.api.CityList;
import org.springframework.social.vkontakte.api.LocationOperations;

/**
 * Implementation VK geolocation API.
 *
 * @see <a href="http://vk.com/dev/places">http://vk.com/dev/places</a>
 * @author badbob
 */
public class LocationTemplate extends AbstractVKontakteOperations implements LocationOperations {

	private final RestTemplate restTemplate;

	public LocationTemplate(RestTemplate restTemplate, String accessToken, ObjectMapper objectMapper, boolean isAuthorizedForUser) {
	        super(isAuthorizedForUser, accessToken, objectMapper);
		this.restTemplate = restTemplate;
	}
	
	@Override
	public City getCityById(Integer id) {
		List<City> cities = getCityById(Collections.singletonList(id));
		return cities.size() > 0 ? cities.get(0) : null;
	}

    @Override
    public List<City> getCityById(Collection<Integer> ids) {
        String idsString = buildCidsAsString(ids);
        Properties props = new Properties();

        props.put("cids", idsString);
        URI uri = makeOperationURL("places.getCityById", props, ApiVersion.VERSION_3_0);

        CityList cityList = restTemplate.getForObject(uri, CityList.class);
        assert cityList != null;
        checkForError(cityList);

        return cityList.getCities();
    }

    // This method compile a coma-separated string from collection of integer city identifier.
    // Actually, this method do the same thing as org.apache.commons.lang.StringUtils.join(ids, ',').
    // But, I decide, that there is to many overheads to include commons-lang lib to use one single method.
	private String buildCidsAsString(Collection<Integer> ids) {
		String delim = "";
{'fraction_non_alphanumeric': 0.09249403560286291, 'fraction_numerical': 0.005872637181134153, 'mean_word_length': 4.225311601150527, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 2}, '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': '6278114', 'n_tokens_mistral': 1536, 'n_tokens_neox': 1469, 'n_words': 462}