欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

范式理论的程序设计(三)

程序员文章站 2022-05-08 11:23:45
...

关于数据库范式理论的程序设计 - CollectionUtils

这里我使用的工具类全部都是使用接口,public interface CollectionUtils extends SequenceUtils是关于属性集合的工具。

Utils工具的继承关系

范式理论的程序设计(三)

CollectionUtils接口

import com.ruoxing.dbs.bean.FunctionalDependency;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
/**
 * @author :ruoxing
 * @type :Interface
 * @date :2018年5月
 * @description :集合工具(转换)
 */

public interface CollectionUtils extends SequenceUtils {
}

范式理论的程序设计(三)

collectionToSet(),collectionToArray(),sequenceToSet(),sequenceToList()方法

1. 方法代码

    /**
     * 描述:字符序列转为List集合(没有去重但保留顺序)
     * @param sequence CharSequence
     * @return List<Character>
     */
    default List<Character> sequenceToList(CharSequence sequence) {
        final List<Character> characters = new ArrayList<>(sequence.length());
        for (int i = 0; i < sequence.length(); i++) {
            characters.add(sequence.charAt(i));
        }
        return characters;
    }
    /**
     * 描述:字符序列转为Set集合(去重但没有保留顺序)
     * @param sequence CharSequence
     * @return Set<Character>
     */
    default Set<Character> sequenceToSet(CharSequence sequence) {
        final Set<Character> characters = new HashSet<>(sequence.length());
        for (int i = 0; i < sequence.length(); i++) {
            characters.add(sequence.charAt(i));
        }
        return characters;
    }

    /**
     * 描述:集合转化为数组
     * @param collection Collection<Character>
     * @return Character[]
     */
    default Character[] collectionToArray(Collection<Character> collection) {
        return collection.toArray(new Character[]{});
    }

    /**
     * 描述:集合转化为Set集合
     * @param collection Collection<Character>
     * @return Set<Character>
     */
    default Set<Character> collectionToSet(Collection<Character> collection) {
        return new HashSet<>(collection);
    }

2. 测试代码(Junit)

import com.ruoxing.dbs.util.CollectionUtils;
import org.junit.Test;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
/**
 * 描述:对CollectionUtils的测试
 */
public class Test2 implements CollectionUtils {

}
    @Test
    public void test01() {
        String A = "ABBC";
        System.out.println("sequenceToSet(A)=" + sequenceToSet(A));//sequenceToSet(A)=[A, B, C]
        System.out.println("sequenceToList(A)=" + sequenceToList(A));//sequenceToList(A)=[A, B, B, C]

        final Collection<Character> C = new ArrayList<>();
        C.add('A');
        C.add('B');
        C.add('B');
        C.add('C');
        System.out.println(C);//[A, B, B, C]
        System.out.println("collectionToSet(C)=" + collectionToSet(C));//collectionToSet(C)=[A, B, C]
        System.out.println("collectionToArray(C)=" + Arrays.toString(collectionToArray(C)));//collectionToArray(C)=[A, B, B, C]
    }

attributionSet(),attributionArray()方法

1. 方法代码

    /**
     * 描述:获取依赖集的属性集
     * @param fds Collection<FunctionalDependency>
     * @return Set<Character>
     */
    default Set<Character> attributionSet(Collection<FunctionalDependency> fds) {
        final Set<Character> characters = new HashSet<>();
        for (FunctionalDependency fd : fds) {
            final String s = "" + fd.getLeft() + fd.getRight();
            for (int i = 0; i < s.length(); i++) {
                characters.add(s.charAt(i));
            }
        }
        return characters;
    }

    /**
     * 描述:获取依赖集的属性集
     * @param fds Collection<FunctionalDependency>
     * @return Character[]
     */
    default Character[] attributionArray(Collection<FunctionalDependency> fds) {
        return attributionSet(fds).toArray(new Character[]{});
    }
    /**
     * 描述:获取依赖集的属性集
     * @param fds Collection<FunctionalDependency>
     * @return String字符序列
     */
    default String attributionSequence(Collection<FunctionalDependency> fds) {
        return collectionToSequence(attributionSet(fds)).toString();
    }
  1. 测试代码
@Test
    public void test02() {
        final List<FunctionalDependency> fds = new ArrayList<>();
        fds.add(new FunctionalDependency("A","BCD"));
        fds.add(new FunctionalDependency("BC","DE"));
        fds.add(new FunctionalDependency("B","D"));
        fds.add(new FunctionalDependency("D","A"));
        System.out.println("属性集 = " + Arrays.toString(attributionArray(fds)));//属性集 = [A, B, C, D, E]
    }