mirror of
				https://github.com/commons-app/apps-android-commons.git
				synced 2025-10-31 14:53:59 +01:00 
			
		
		
		
	Convert Entities to kotlin
This commit is contained in:
		
							parent
							
								
									a3bad03e3c
								
							
						
					
					
						commit
						9d1c2d3a51
					
				
					 2 changed files with 64 additions and 106 deletions
				
			
		|  | @ -1,106 +0,0 @@ | |||
| package fr.free.nrw.commons.wikidata.model; | ||||
| 
 | ||||
| import androidx.annotation.NonNull; | ||||
| import androidx.annotation.Nullable; | ||||
| import com.google.gson.annotations.SerializedName; | ||||
| import java.util.Collections; | ||||
| import java.util.List; | ||||
| import java.util.Map; | ||||
| import org.apache.commons.lang3.StringUtils; | ||||
| import org.jetbrains.annotations.NotNull; | ||||
| import fr.free.nrw.commons.wikidata.mwapi.MwResponse; | ||||
| 
 | ||||
| 
 | ||||
| public class Entities extends MwResponse { | ||||
|     @Nullable private Map<String, Entity> entities; | ||||
|     private int success; | ||||
| 
 | ||||
|     @NotNull | ||||
|     public Map<String, Entity> entities() { | ||||
|         return entities != null ? entities : Collections.emptyMap(); | ||||
|     } | ||||
| 
 | ||||
|     public int getSuccess() { | ||||
|         return success; | ||||
|     } | ||||
| 
 | ||||
|     @Nullable public Entity getFirst() { | ||||
|         if (entities == null) { | ||||
|             return null; | ||||
|         } | ||||
|         return entities.values().iterator().next(); | ||||
|     } | ||||
| 
 | ||||
|     @Override | ||||
|     public void postProcess() { | ||||
|         if (getFirst() != null && getFirst().isMissing()) { | ||||
|             throw new RuntimeException("The requested entity was not found."); | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
|     public static class Entity { | ||||
|         @Nullable private String type; | ||||
|         @Nullable private String id; | ||||
|         @Nullable private Map<String, Label> labels; | ||||
|         @Nullable private Map<String, Label> descriptions; | ||||
|         @Nullable private Map<String, SiteLink> sitelinks; | ||||
|         @Nullable @SerializedName(value = "statements", alternate = "claims") private Map<String, List<StatementPartial>> statements; | ||||
|         @Nullable private String missing; | ||||
| 
 | ||||
|         @NonNull public String id() { | ||||
|             return StringUtils.defaultString(id); | ||||
|         } | ||||
| 
 | ||||
|         @NonNull public Map<String, Label> labels() { | ||||
|             return labels != null ? labels : Collections.emptyMap(); | ||||
|         } | ||||
| 
 | ||||
|         @NonNull public Map<String, Label> descriptions() { | ||||
|             return descriptions != null ? descriptions : Collections.emptyMap(); | ||||
|         } | ||||
| 
 | ||||
|         @NonNull public Map<String, SiteLink> sitelinks() { | ||||
|             return sitelinks != null ? sitelinks : Collections.emptyMap(); | ||||
|         } | ||||
| 
 | ||||
|         @Nullable | ||||
|         public Map<String, List<StatementPartial>> getStatements() { | ||||
|             return statements; | ||||
|         } | ||||
| 
 | ||||
|         boolean isMissing() { | ||||
|             return "-1".equals(id) && missing != null; | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
|     public static class Label { | ||||
|         @Nullable private String language; | ||||
|         @Nullable private String value; | ||||
| 
 | ||||
|         public Label(@Nullable final String language, @Nullable final String value) { | ||||
|             this.language = language; | ||||
|             this.value = value; | ||||
|         } | ||||
| 
 | ||||
|         @NonNull public String language() { | ||||
|             return StringUtils.defaultString(language); | ||||
|         } | ||||
| 
 | ||||
|         @NonNull public String value() { | ||||
|             return StringUtils.defaultString(value); | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
|     public static class SiteLink { | ||||
|         @Nullable private String site; | ||||
|         @Nullable private String title; | ||||
| 
 | ||||
|         @NonNull public String getSite() { | ||||
|             return StringUtils.defaultString(site); | ||||
|         } | ||||
| 
 | ||||
|         @NonNull public String getTitle() { | ||||
|             return StringUtils.defaultString(title); | ||||
|         } | ||||
|     } | ||||
| } | ||||
|  | @ -0,0 +1,64 @@ | |||
| package fr.free.nrw.commons.wikidata.model | ||||
| 
 | ||||
| import com.google.gson.annotations.SerializedName | ||||
| import fr.free.nrw.commons.wikidata.mwapi.MwResponse | ||||
| import org.apache.commons.lang3.StringUtils | ||||
| 
 | ||||
| class Entities : MwResponse() { | ||||
|     private val entities: Map<String, Entity>? = null | ||||
|     val success: Int = 0 | ||||
| 
 | ||||
|     fun entities(): Map<String, Entity> = entities ?: emptyMap() | ||||
| 
 | ||||
|     private val first : Entity? | ||||
|         get() = entities?.values?.iterator()?.next() | ||||
| 
 | ||||
|     override fun postProcess() { | ||||
|         first?.let { | ||||
|             if (it.isMissing()) throw RuntimeException("The requested entity was not found.") | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
|     class Entity { | ||||
|         private val type: String? = null | ||||
|         private val id: String? = null | ||||
|         private val labels: Map<String, Label>? = null | ||||
|         private val descriptions: Map<String, Label>? = null | ||||
|         private val sitelinks: Map<String, SiteLink>? = null | ||||
| 
 | ||||
|         @SerializedName(value = "statements", alternate = ["claims"]) | ||||
|         val statements: Map<String, List<StatementPartial>>? = null | ||||
|         private val missing: String? = null | ||||
| 
 | ||||
|         fun id(): String = | ||||
|             StringUtils.defaultString(id) | ||||
| 
 | ||||
|         fun labels(): Map<String, Label> = | ||||
|             labels ?: emptyMap() | ||||
| 
 | ||||
|         fun descriptions(): Map<String, Label> = | ||||
|             descriptions ?: emptyMap() | ||||
| 
 | ||||
|         fun sitelinks(): Map<String, SiteLink> = | ||||
|             sitelinks ?: emptyMap() | ||||
| 
 | ||||
|         fun isMissing(): Boolean = | ||||
|             "-1" == id && missing != null | ||||
|     } | ||||
| 
 | ||||
|     class Label(private val language: String?, private val value: String?) { | ||||
|         fun language(): String = | ||||
|             StringUtils.defaultString(language) | ||||
| 
 | ||||
|         fun value(): String = | ||||
|             StringUtils.defaultString(value) | ||||
|     } | ||||
| 
 | ||||
|     class SiteLink { | ||||
|         val site: String? = null | ||||
|             get() = StringUtils.defaultString(field) | ||||
| 
 | ||||
|         private val title: String? = null | ||||
|             get() = StringUtils.defaultString(field) | ||||
|     } | ||||
| } | ||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Paul Hawke
						Paul Hawke