You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
123456/java/net/micode/notes/gtask/data/MetaData.java

107 lines
5.3 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

/*
* Copyright (c) 2010-2011, The MiCode Open Source Community (www.micode.net)
*
* 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.
* 总体分析
这段 Java 代码定义了MetaData类它继承自Task类主要用于处理与元数据相关的特定逻辑聚焦在关联 GTask 的相关标识通过mRelatedGid以及对元数据信息的存储、获取和相关操作的控制上。类中提供了设置元数据的方法同时重写了父类的一些方法像判断是否值得保存以及根据远程 JSON 数据设置内容等方法,不过也限制了部分方法(如设置本地 JSON 内容、获取本地 JSON 内容以及获取同步操作等方法)不能被调用,整体体现了对元数据这种特定数据在应用特定业务场景下的特殊处理逻辑。
函数分析
setMeta方法
所属类MetaData
功能尝试将传入的gid关联的 GTask 标识添加到给定的JSONObject元数据信息中指定的键GTaskStringUtils.META_HEAD_GTASK_ID若添加过程出现JSONException则记录错误日志最后通过调用父类的setNotes方法将处理后的JSONObject转换为字符串进行存储并设置名称为特定的元数据名称GTaskStringUtils.META_NOTE_NAME用于设置元数据相关信息并关联对应的 GTask 标识。
getRelatedGid方法
所属类MetaData
功能:返回记录的关联 GTask 的标识mRelatedGid用于获取元数据所关联的 GTask 相关标识信息。
isWorthSaving方法
所属类MetaData
功能重写了父类的该方法通过判断通过getNotes方法获取到的元数据内容是否为null返回表示是否值得保存的布尔值用于确定当前元数据对象是否有需要保存的数据内容。
setContentByRemoteJSON方法
所属类MetaData
功能首先调用父类的setContentByRemoteJSON方法然后在确保获取到的元数据内容不为null的情况下尝试将其转换为JSONObject并从中获取关联 GTask 的标识mRelatedGid若获取过程出现JSONException则记录警告日志并将mRelatedGid设为null用于根据远程传来的 JSON 数据设置元数据内容并解析出关联的 GTask 标识。
setContentByLocalJSON方法
所属类MetaData
功能明确抛出IllegalAccessError异常表示该方法不应被调用用于限制该方法在当前类中的使用情况体现特定业务逻辑下对本地 JSON 设置元数据内容操作的禁止。
getLocalJSONFromContent方法
所属类MetaData
功能抛出IllegalAccessError异常表明该方法不应被调用用于阻止在当前类中执行从内容获取本地 JSON 的操作,同样是基于特定业务逻辑对该功能的限制。
getSyncAction方法
所属类MetaData
功能抛出IllegalAccessError异常意味着该方法不应被调用用于限定在该类中不能进行获取同步操作相关的调用体现对元数据在同步操作方面的特定处理规则。
*/
package net.micode.notes.gtask.data;
import android.database.Cursor;
import android.util.Log;
import net.micode.notes.tool.GTaskStringUtils;
import org.json.JSONException;
import org.json.JSONObject;
public class MetaData extends Task {
private final static String TAG = MetaData.class.getSimpleName();
private String mRelatedGid = null;
public void setMeta(String gid, JSONObject metaInfo) {
try {
metaInfo.put(GTaskStringUtils.META_HEAD_GTASK_ID, gid);
} catch (JSONException e) {
Log.e(TAG, "failed to put related gid");
}
setNotes(metaInfo.toString());
setName(GTaskStringUtils.META_NOTE_NAME);
}
public String getRelatedGid() {
return mRelatedGid;
}
@Override
public boolean isWorthSaving() {
return getNotes() != null;
}
@Override
public void setContentByRemoteJSON(JSONObject js) {
super.setContentByRemoteJSON(js);
if (getNotes() != null) {
try {
JSONObject metaInfo = new JSONObject(getNotes().trim());
mRelatedGid = metaInfo.getString(GTaskStringUtils.META_HEAD_GTASK_ID);
} catch (JSONException e) {
Log.w(TAG, "failed to get related gid");
mRelatedGid = null;
}
}
}
@Override
public void setContentByLocalJSON(JSONObject js) {
// this function should not be called
throw new IllegalAccessError("MetaData:setContentByLocalJSON should not be called");
}
@Override
public JSONObject getLocalJSONFromContent() {
throw new IllegalAccessError("MetaData:getLocalJSONFromContent should not be called");
}
@Override
public int getSyncAction(Cursor c) {
throw new IllegalAccessError("MetaData:getSyncAction should not be called");
}
}