Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/*
* Copyright 2016 Google Inc. All Rights Reserved.
*
* 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 com.google.cloud.translate;

import com.google.api.services.translate.model.LanguagesResource;
import com.google.common.base.Function;
import com.google.common.base.MoreObjects;

import java.io.Serializable;
import java.util.Objects;

/**
* Information about a language supported by Google Translate. Objects of this class contain
* language's code and the language name.
*
* @see <a href="https://cloud.google.com/translate/v2/discovering-supported-languages-with-rest">
* Discovering Supported Languages</a>
* @see <a href="https://cloud.google.com/translate/v2/translate-reference#supported_languages">
* Supported Languages</a>
*/
public class Language implements Serializable {

private static final long serialVersionUID = 5205240279371907020L;
static final Function<LanguagesResource, Language> FROM_PB_FUNCTION =
new Function<LanguagesResource, Language>() {
@Override
public Language apply(LanguagesResource languagePb) {
return Language.fromPb(languagePb);
}
};

private final String code;
private final String name;

private Language(String code, String name) {
this.code = code;
this.name = name;
}

/**
* Returns the code of the language.
*/
public String code() {
return code;
}

/**
* Returns the name of the language.
*/
public String name() {
return name;
}

@Override
public String toString() {
return MoreObjects.toStringHelper(this)
.add("code", code)
.add("name", name)
.toString();
}

@Override
public final int hashCode() {
return Objects.hash(code, name);
}

@Override
public final boolean equals(Object obj) {
if (obj == this) {
return true;
}
if (obj == null || !obj.getClass().equals(Language.class)) {
return false;
}
Language other = (Language) obj;
return Objects.equals(code, other.code)
&& Objects.equals(name, other.name);
}

static Language fromPb(LanguagesResource languagePb) {
return new Language(languagePb.getLanguage(), languagePb.getName());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Copyright 2016 Google Inc. All Rights Reserved.
*
* 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 com.google.cloud.translate;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;

import com.google.api.services.translate.model.LanguagesResource;

import org.junit.Test;

public class LanguageTest {

private static final String CODE = "en";
private static final String NAME = "English";
private static final LanguagesResource LANGUAGE_PB =
new LanguagesResource().setLanguage(CODE).setName(NAME);
private static final Language LANGUAGE = Language.fromPb(LANGUAGE_PB);

@Test
public void testFromPb() {
assertEquals(CODE, LANGUAGE.code());
assertEquals(NAME, LANGUAGE.name());
Language language = Language.fromPb(new LanguagesResource().setLanguage(CODE));
assertEquals(CODE, language.code());
assertNull(language.name());
compareLanguage(LANGUAGE, Language.fromPb(LANGUAGE_PB));
}

private void compareLanguage(Language expected, Language value) {
assertEquals(expected, value);
assertEquals(expected.name(), value.name());
assertEquals(expected.code(), value.code());
assertEquals(expected.hashCode(), value.hashCode());
assertEquals(expected.toString(), value.toString());
}
}