diff options
author | Xavier Ducrohet <xav@android.com> | 2011-02-25 10:34:06 -0800 |
---|---|---|
committer | Xavier Ducrohet <xav@android.com> | 2011-02-25 10:34:06 -0800 |
commit | 2a66e728a48e59cafc749b80552fb4121b212d78 (patch) | |
tree | 5031d31f732adf5eae83c175f693681ef9871d57 /common | |
parent | cef4bf36760dd776ddab69652beee255c854283b (diff) | |
download | sdk-2a66e728a48e59cafc749b80552fb4121b212d78.zip sdk-2a66e728a48e59cafc749b80552fb4121b212d78.tar.gz sdk-2a66e728a48e59cafc749b80552fb4121b212d78.tar.bz2 |
Move FolderTypeRelationship to common.jar
Change-Id: I3a151d2fadd1c1e34177cb729fe5545fa754849a
Diffstat (limited to 'common')
-rw-r--r-- | common/Android.mk | 3 | ||||
-rw-r--r-- | common/src/com/android/resources/FolderTypeRelationship.java | 164 | ||||
-rw-r--r-- | common/tests/.classpath | 8 | ||||
-rw-r--r-- | common/tests/.project | 17 | ||||
-rw-r--r-- | common/tests/Android.mk | 27 | ||||
-rw-r--r-- | common/tests/src/com/android/resources/FolderTypeRelationShipTest.java | 44 |
6 files changed, 263 insertions, 0 deletions
diff --git a/common/Android.mk b/common/Android.mk index aa2ba8e..ef907f2 100644 --- a/common/Android.mk +++ b/common/Android.mk @@ -25,3 +25,6 @@ LOCAL_MODULE := common LOCAL_MODULE_TAGS := optional include $(BUILD_HOST_JAVA_LIBRARY) + +# Build all sub-directories +include $(call all-makefiles-under,$(LOCAL_PATH)) diff --git a/common/src/com/android/resources/FolderTypeRelationship.java b/common/src/com/android/resources/FolderTypeRelationship.java new file mode 100644 index 0000000..34961a3 --- /dev/null +++ b/common/src/com/android/resources/FolderTypeRelationship.java @@ -0,0 +1,164 @@ +/* + * Copyright (C) 2007 The Android Open Source Project + * + * 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.android.resources; + + +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +/** + * This class gives access to the bidirectional relationship between {@link ResourceType} and + * {@link ResourceFolderType}. + */ +public final class FolderTypeRelationship { + + private final static Map<ResourceType, List<ResourceFolderType>> mTypeToFolderMap = + new HashMap<ResourceType, List<ResourceFolderType>>(); + + private final static Map<ResourceFolderType, List<ResourceType>> mFolderToTypeMap = + new HashMap<ResourceFolderType, List<ResourceType>>(); + + static { + // generate the relationships in a temporary map + add(ResourceType.ANIM, ResourceFolderType.ANIM); + add(ResourceType.ANIMATOR, ResourceFolderType.ANIMATOR); + add(ResourceType.ARRAY, ResourceFolderType.VALUES); + add(ResourceType.ATTR, ResourceFolderType.VALUES); + add(ResourceType.BOOL, ResourceFolderType.VALUES); + add(ResourceType.COLOR, ResourceFolderType.VALUES); + add(ResourceType.COLOR, ResourceFolderType.COLOR); + add(ResourceType.DECLARE_STYLEABLE, ResourceFolderType.VALUES); + add(ResourceType.DIMEN, ResourceFolderType.VALUES); + add(ResourceType.DRAWABLE, ResourceFolderType.VALUES); + add(ResourceType.DRAWABLE, ResourceFolderType.DRAWABLE); + add(ResourceType.FRACTION, ResourceFolderType.VALUES); + add(ResourceType.ID, ResourceFolderType.VALUES); + add(ResourceType.INTEGER, ResourceFolderType.VALUES); + add(ResourceType.INTERPOLATOR, ResourceFolderType.INTERPOLATOR); + add(ResourceType.LAYOUT, ResourceFolderType.LAYOUT); + add(ResourceType.MENU, ResourceFolderType.MENU); + add(ResourceType.MIPMAP, ResourceFolderType.MIPMAP); + add(ResourceType.PLURALS, ResourceFolderType.VALUES); + add(ResourceType.PUBLIC, ResourceFolderType.VALUES); + add(ResourceType.RAW, ResourceFolderType.RAW); + add(ResourceType.STRING, ResourceFolderType.VALUES); + add(ResourceType.STYLE, ResourceFolderType.VALUES); + add(ResourceType.STYLEABLE, ResourceFolderType.VALUES); + add(ResourceType.XML, ResourceFolderType.XML); + + makeSafe(); + } + + /** + * Returns a list of {@link ResourceType}s that can be generated from files inside a folder + * of the specified type. + * @param folderType The folder type. + * @return a list of {@link ResourceType}, possibly empty but never null. + */ + public static List<ResourceType> getRelatedResourceTypes(ResourceFolderType folderType) { + List<ResourceType> list = mFolderToTypeMap.get(folderType); + if (list != null) { + return list; + } + + return Collections.emptyList(); + } + + /** + * Returns a list of {@link ResourceFolderType} that can contain files generating resources + * of the specified type. + * @param resType the type of resource. + * @return a list of {@link ResourceFolderType}, possibly empty but never null. + */ + public static List<ResourceFolderType> getRelatedFolders(ResourceType resType) { + List<ResourceFolderType> list = mTypeToFolderMap.get(resType); + if (list != null) { + return list; + } + + return Collections.emptyList(); + } + + /** + * Returns true if the {@link ResourceType} and the {@link ResourceFolderType} values match. + * @param resType the resource type. + * @param folderType the folder type. + * @return true if files inside the folder of the specified {@link ResourceFolderType} + * could generate a resource of the specified {@link ResourceType} + */ + public static boolean match(ResourceType resType, ResourceFolderType folderType) { + List<ResourceFolderType> list = mTypeToFolderMap.get(resType); + + if (list != null) { + return list.contains(folderType); + } + + return false; + } + + /** + * Adds a {@link ResourceType} - {@link ResourceFolderType} relationship. this indicates that + * a file in the folder can generate a resource of the specified type. + * @param type The resourceType + * @param folder The {@link ResourceFolderType} + */ + private static void add(ResourceType type, ResourceFolderType folder) { + // first we add the folder to the list associated with the type. + List<ResourceFolderType> folderList = mTypeToFolderMap.get(type); + if (folderList == null) { + folderList = new ArrayList<ResourceFolderType>(); + mTypeToFolderMap.put(type, folderList); + } + if (folderList.indexOf(folder) == -1) { + folderList.add(folder); + } + + // now we add the type to the list associated with the folder. + List<ResourceType> typeList = mFolderToTypeMap.get(folder); + if (typeList == null) { + typeList = new ArrayList<ResourceType>(); + mFolderToTypeMap.put(folder, typeList); + } + if (typeList.indexOf(type) == -1) { + typeList.add(type); + } + } + + /** + * Makes the maps safe by replacing the current list values with unmodifiable lists. + */ + private static void makeSafe() { + for (ResourceType type : ResourceType.values()) { + List<ResourceFolderType> list = mTypeToFolderMap.get(type); + if (list != null) { + // replace with a unmodifiable list wrapper around the current list. + mTypeToFolderMap.put(type, Collections.unmodifiableList(list)); + } + } + + for (ResourceFolderType folder : ResourceFolderType.values()) { + List<ResourceType> list = mFolderToTypeMap.get(folder); + if (list != null) { + // replace with a unmodifiable list wrapper around the current list. + mFolderToTypeMap.put(folder, Collections.unmodifiableList(list)); + } + } + } +} diff --git a/common/tests/.classpath b/common/tests/.classpath new file mode 100644 index 0000000..b793adc --- /dev/null +++ b/common/tests/.classpath @@ -0,0 +1,8 @@ +<?xml version="1.0" encoding="UTF-8"?> +<classpath> + <classpathentry kind="src" path="src"/> + <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/> + <classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/3"/> + <classpathentry combineaccessrules="false" kind="src" path="/common"/> + <classpathentry kind="output" path="bin"/> +</classpath> diff --git a/common/tests/.project b/common/tests/.project new file mode 100644 index 0000000..9f550a3 --- /dev/null +++ b/common/tests/.project @@ -0,0 +1,17 @@ +<?xml version="1.0" encoding="UTF-8"?> +<projectDescription> + <name>common-tests</name> + <comment></comment> + <projects> + </projects> + <buildSpec> + <buildCommand> + <name>org.eclipse.jdt.core.javabuilder</name> + <arguments> + </arguments> + </buildCommand> + </buildSpec> + <natures> + <nature>org.eclipse.jdt.core.javanature</nature> + </natures> +</projectDescription> diff --git a/common/tests/Android.mk b/common/tests/Android.mk new file mode 100644 index 0000000..10e75e8 --- /dev/null +++ b/common/tests/Android.mk @@ -0,0 +1,27 @@ +# Copyright (C) 2011 The Android Open Source Project +# +# 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. + +LOCAL_PATH := $(call my-dir) + +include $(CLEAR_VARS) + +# Only compile source java files in this lib. +LOCAL_SRC_FILES := $(call all-java-files-under, src) + +LOCAL_MODULE := common-tests +LOCAL_MODULE_TAGS := optional + +LOCAL_JAVA_LIBRARIES := common junit + +include $(BUILD_HOST_JAVA_LIBRARY) diff --git a/common/tests/src/com/android/resources/FolderTypeRelationShipTest.java b/common/tests/src/com/android/resources/FolderTypeRelationShipTest.java new file mode 100644 index 0000000..809eae7 --- /dev/null +++ b/common/tests/src/com/android/resources/FolderTypeRelationShipTest.java @@ -0,0 +1,44 @@ +/* + * Copyright (C) 2010 The Android Open Source Project + * + * 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.android.resources; + +import com.android.resources.FolderTypeRelationship; +import com.android.resources.ResourceFolderType; +import com.android.resources.ResourceType; + +import junit.framework.TestCase; + +public class FolderTypeRelationShipTest extends TestCase { + + public void testResourceType() { + // all resource type should be in the FolderTypeRelationShip map. + // loop on all the enum, and make sure there's at least one folder type for it. + for (ResourceType type : ResourceType.values()) { + assertTrue(type.getDisplayName(), + FolderTypeRelationship.getRelatedFolders(type).size() > 0); + } + } + + public void testResourceFolderType() { + // all resource folder type should generate at least one type of resource. + // loop on all the enum, and make sure there's at least one res type for it. + for (ResourceFolderType type : ResourceFolderType.values()) { + assertTrue(type.getName(), + FolderTypeRelationship.getRelatedResourceTypes(type).size() > 0); + } + } +} |